Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,687 members, 7,809,599 topics. Date: Friday, 26 April 2024 at 11:50 AM

Trivia Coding Questions (euler Project) - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Trivia Coding Questions (euler Project) (9900 Views)

Learn Coding!!! Weekdays And Weekend / Are You A Programmer? Answer This NCT Coding Questions! / Naija Coder: Test Your Coding Skills Now (2) (3) (4)

(1) (2) (3) (4) (Reply) (Go Down)

Re: Trivia Coding Questions (euler Project) by olyjosh(m): 3:26am On May 02, 2015
WhiZTiM:
. . . .Happy Coding ....
. . .As for the prime number generation, you may want to try Sieve of Eratosthenes... or other Primality tests...

...my favorite, "...most prime numbers from 3 and above obeys ....(6k + 1) or (6k - 1), where k is a natural number"... THat should speed up implementation to some extent....

Thanks for reminding me of Sieve of Eratosthenes. That will work fine for the example test case but not for natural number that is as big as 600851475143, it wont work in java since the largest lenght of array or Set(and Set implementation) you can have is 2^31 (int precision) and Sieve of Eratosthenes reqires boolean flagging over such array.
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 3:29am On May 02, 2015
WhiZTiM:

@olyjosh.
I am really impressed! ...Would love to chat up with you Sir.

Nice meeting you Bro

1 Like

Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 3:48am On May 02, 2015
olyjosh:


Thanks for reminding me of Sieve of Eratosthenes. That will work fine for the example test case but not for natural number that is as big as 600851475143, it wont work in java since the largest lenght of array or Set(and Set implementation) you can have is 2^31 (int precision) and Sieve of Eratosthenes reqires boolean flagging over such array.

...oh right... You guys don't have bitarrays in Java? ...Ouchhh
....Another optimization (rough thought) ... Its quite expensive, but you can split up the generation process. Generate them in chunks: then restart using higher values...
First 30 primes, next 30 primes, next 30 primes... ...and so on.... so, after 90, for example.

You can order this 90, and when a search for prime is needed, just do a binary search on this 90.
(easier said than done, #haha)
Re: Trivia Coding Questions (euler Project) by Nobody: 11:56am On May 02, 2015
kudaisi:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
in php
function getSum($u){
for ($uu = 0 ; $uu < 1000 ; $uu++){
if ($uu % 3 === 0 || $uu % 5 === 0 ){
$u += $uu ;
}
}
return $u
}
echo getSum(0) ; // 233168
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 3:59pm On May 02, 2015
WhiZTiM:
Comparing the Runtime of olyjosh's simple and elegant solution, (Java) with mine, (Python)
You can see some abominable things happening... Python 3x faster than Java. ....HOw?
Memorization. smiley ....

http://ideone.com/XNVT5m ....Java (greedy) ...0.07seconds 320MB
http://ideone.com/VSTUQe .....Python (Dynamic Programming) ...0.02seconds 8MB

Java will regain its glory if DP/Memorization is used.



Cool. I will tryimplementing this with DP. Haaaa, but I doubt if i understand most of this python Types and sytax correctly.
Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 5:23pm On May 02, 2015
olyjosh:
public class LargestPrimeFactor {
static boolean isPrime(long n){
if(n==1)return false;
if(n==2)return true;
if(n%2==0) return false;
int max = (int)Math.sqrt(n);
for (int i = 2; i <= max; i++) {
if(n%i==0)return false;
}
return true;
}

static Queue<Long> factors(long n){
Queue fac = new ArrayDeque();
//ArrayList<Long> factors = new ArrayList<>();
long h=Math.round(n/2);
for (long i = 2; i <= h; i++) {
if(n%i==0){fac.add(i);}
}
return fac;
}

public static void main(String[] args) {
long lastPrime=0;
long n =600851475143L;
Queue<Long> factors = factors(n);
for (Iterator<Long> iterator = factors.iterator(); iterator.hasNext()wink {
long next = iterator.next();
if(isPrime(next))lastPrime=next;
}
System.out.println("largest prime factor is: "+lastPrime);
}
}


largest prime factor is: 6857
This solution got me scared cause it runs for about 2minnutes on my 4gb RAM, 1.65GHz processor
Nice... simple and elegant.

We could do a little more better... ...requires more code though...

Still a bit of a DP problem... My implementation (inefficient) works in about 11.78seconds
On my PC, a 1.7Ghz CPU (Core i5 4210U).

On an interesting note, I ported the implementation to Python... and its been running for the past 15 minutes! ...I am still yet to get an answer.
. . ..lolz. ... smiley ...Despite using C array type... I can only offer sympathy to what Ruby will be like....

...ok, here's it
http://ideone.com/6Iu0fJ (13.44 seconds)

Edit: See the efficient version in my next comment https://www.nairaland.com/2286523/trivia-coding-questions-euler-project/1#33393053
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 12:44am On May 03, 2015
@WhiZTiM check this out
Runs for 10seconds on 4gb RAM, 1.65GHz Duo Core processor



public class Primes
{
static boolean isPrime(long n)
{
if ( n % 2 == 0 )return false;
long sqrt = (long) Math.sqrt(n);
sqrt= sqrt%2 == 0 ? sqrt-1 : sqrt;
for ( int i = 3; i < sqrt; i += 2 )
{
if ( n % i == 0 )return false;
}
return true;
}

public static void main(String[] args)
{

long t = 600851475143L;
long d = 2;
while (1==1)
{
long tmp = 600851475143L / d;
if ( t % tmp == 0 && isPrime(tmp) )
{
System.out.println("= " + tmp);
break;
}
d++;
}
}
}


Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 12:54am On May 03, 2015
olyjosh:
@WhiZTiM check this out
Runs for 10seconds on 4gb RAM, 1.65GHz Duo Core processor



public class Primes
{
static boolean isPrime(long n)
{
if ( n % 2 == 0 )return false;
long sqrt = (long) Math.sqrt(n);
sqrt= sqrt%2 == 0 ? sqrt-1 : sqrt;
for ( int i = 3; i < sqrt; i += 2 )
{
if ( n % i == 0 )return false;
}
return true;
}

public static void main(String[] args)
{

long t = 600851475143L;
long d = 2;
while (1==1)
{
long tmp = 600851475143L / d;
if ( t % tmp == 0 && isPrime(tmp) )
{
System.out.println("= " + tmp);
break;
}
d++;
}
}
}



The division sequence....
A bit more clever solution! +1.
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 9:07am On May 03, 2015
Jregz:

in php
function getSum($u){
for ($uu = 0 ; $uu < 1000 ; $uu++){
if ($uu % 3 === 0 || $uu % 5 === 0 ){
$u += $uu ;
}
}
return $u
}
echo getSum(0) ; // 233168

Welcome to the show
Re: Trivia Coding Questions (euler Project) by Nobody: 10:36am On May 03, 2015
olyjosh:


Welcome to the show

wow you guys really rocks please any links to jazz up my algorithm skills. Might need to branch out from the business development and do some Maths related ish for 3D on Web my maths is abit bad. keep it up guys.
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 10:53am On May 03, 2015
kudaisi:
The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.



/**
*
* @author olyjosh
*/
public class DifferenceSumsSquare {

public static void main(String[] args) {
final int n=100;
int sqOfSum = (n*(n+1)*(2*n+1))/6;
int sumOfsq = (n*(1+n))/2; sumOfsq*=sumOfsq;
System.out.println("Difference is "+(sumOfsq-sqOfSum));
}
}


Difference is 25164150
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:30am On May 03, 2015
kudaisi:
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

public class _10001stPrime {

static boolean isPrime(long n)
{
if ( n % 2 == 0 )return false;
long sqrt = (long) Math.sqrt(n);
sqrt= sqrt%2 == 0 ? sqrt-1 : sqrt;
for ( int i = 3; i < sqrt; i += 2 )
if ( n%i == 0 )return false;
return true;
}

public static void main(String[] args) {
int i =1;
int n =3;
while(i<10001){
if(isPrime(n))i++;
n+=2;
}
System.out.println("10001st prime number: "+n);
}
}

10001st prime number: 103575

I out for now - Gat exams to write tommorow. BRB After.
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 12:34pm On May 03, 2015
Sorry I wish I could delete the above implementation, It's buggy, BRB when I fix it
Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 11:12pm On May 03, 2015
olyjosh:


Thanks for reminding me of Sieve of Eratosthenes. That will work fine for the example test case but not for natural number that is as big as 600851475143, it wont work in java since the largest lenght of array or Set(and Set implementation) you can have is 2^31 (int precision) and Sieve of Eratosthenes reqires boolean flagging over such array.

Sieve of Eratosthenes is actually the best for this problem. A colleague of mine pointed that out to me. It works in 0.01 seconds on my PC. C++.
Checkout:
http://ideone.com/n0CVvD
...
Re: Trivia Coding Questions (euler Project) by blueyedgeek(m): 12:00am On May 04, 2015
kudaisi:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
 
var sum = 0;
(function () {
for (var i = 0; i < 1000; i += 1) {
if (i % 5 === 0 || i % 3 === 0) {
sum += i;
}
}
return sum;
} () ) ;
// 233168
Re: Trivia Coding Questions (euler Project) by blueyedgeek(m): 12:29am On May 04, 2015
kudaisi:
The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
 
(function () {
var sum = 0;
for (var i = 0; i <= 100; i += 1) {
sum += i;
}
return Math.pow(sum, 2);
} ()) - (function () {
var sum = 0;
for (var i = 0; i <= 100; i += 1) {
sum += Math.pow(i, 2);
}
return sum;
} () )
// 25164150
Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 1:47am On May 04, 2015
kudaisi:
The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Python 2.7x
>>> (50*(2+99))**2 - reduce(lambda x, y: x + y**2, range(101))
Ans: 25164150
One liner ...:-) .... The magic values to the left of the subtraction are from the AP series summation formula
Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 1:47am On May 04, 2015
deleted!
Re: Trivia Coding Questions (euler Project) by kudaisi(m): 9:44am On May 04, 2015
The sum of the squares of the first ten natural numbers is,

(see image for inserts)
The square of the sum of the first ten natural numbers is,

(see image for inserts)
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:19am On May 04, 2015
blueyedgeek:

 
(function () {
var sum = 0;
for (var i = 0; i <= 100; i += 1) {
sum += i;
}
return Math.pow(sum, 2);
} ()) - (function () {
var sum = 0;
for (var i = 0; i <= 100; i += 1) {
sum += Math.pow(i, 2);
}
return sum;
} () )
// 25164150

Cool implementation bro. But the best approach to this isn't greedy approach. The sum arithmetic progression is what you can use in both case. These are formula you can easily derive using mathematical inductions.

Sn = {n(1+n)}/2
Sum of squres in AP is = {n(n+1)(2*n+1)}/6
where one 1 can alway be the first term if your series does not start from 1 and n is the last term
Re: Trivia Coding Questions (euler Project) by kudaisi(m): 12:03pm On May 04, 2015
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?
Re: Trivia Coding Questions (euler Project) by kudaisi(m): 12:26pm On May 04, 2015
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
Re: Trivia Coding Questions (euler Project) by Borwe: 2:47pm On May 04, 2015
kudaisi:
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

What does this question mean LOL cheesy cheesy cheesy
Re: Trivia Coding Questions (euler Project) by kudaisi(m): 4:29pm On May 04, 2015
Borwe:


What does this question mean LOL cheesy cheesy cheesy
The set of numbers are part of single 1000-digit number. if you look through carefully, on the thirteenth row you'll notice a combination of 9989 together within the 1000-digit number and they have the greatest product because no other product (multiplication) of 4 adjacent digits(numbers that appear side by side) is greater that 5832. Now, your required to find 13 adjacent numbers with the highest product. Clear now ?
Re: Trivia Coding Questions (euler Project) by kudaisi(m): 4:35pm On May 04, 2015
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

(see image for inserts)
For example, (see image for inserts)

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Re: Trivia Coding Questions (euler Project) by andreT(m): 6:38pm On May 04, 2015
python
def pythagoras(limit):
for i in range(1, limit):
for j in range(1, limit):
for k in range(1, limit):

if i**2 + j** 2 == k**2:
if i + j + k == 1000:
print i, j, k
print i * j * k
return True

pythagoras(500)
Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 7:04pm On May 04, 2015
kudaisi:
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

Full, working program here: http://ideone.com/0S6NVj
Largest value at index: 503
Sequence is: 9 x 7 x 8 x 1 x 7 x 9 x 7 x 7 x 8 x 4 x 6 x 1 x 7 = 2091059712

[s]Runtime: 0.000199762 seconds;
Memory: 3.28MB[/s]
Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 8:18pm On May 04, 2015
kudaisi:
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

(see image for inserts)
For example, (see image for inserts)

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Here, a non-optimal solution... but it works
http://ideone.com/qxpoTH
http://ideone.com/upxEf2

Answer: 200, 375, 425; Product: 31875000
Runtime: 0.1 second
Memory: 3.0MB

Side Note: I initially implemented it in Python, but it ran for 2 minutes... :-)
So, I was pretty sure that other than machine code, the C++ compiler will emit vectorized instructions.... and some other optimizations, loop unrolling... blah nlah blah....
Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 8:23pm On May 04, 2015
andreT:
python
def pythagoras(limit):
for i in range(1, limit):
for j in range(1, limit):
for k in range(1, limit):

if i**2 + j** 2 == k**2:
if i + j + k == 1000:
print i, j, k
print i * j * k
return True

pythagoras(500)

Now that is a perfect cubic runtime....
Don't do that!. .....at least in Python.... :-) ....(this is gonna run for minutes)
PS: Don't take me too serious....

Ehmm... there should be known methods or algorithm that generates pythagorean triples... Google that.... ...implement it and woolala... it will run under a second.

1 Like

Re: Trivia Coding Questions (euler Project) by olyjosh(m): 12:18am On May 05, 2015
kudaisi:
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

(see image for inserts)
For example, (see image for inserts)

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

/**
*
* @author olyjosh
*/
public class PythagoreanTriplet {

public static void main(String[] args) {
int lim=1000/3;
for (int i = 2; i <=lim; i++) {
for (int j = i+1; j <= lim; j++) {
int c = j*j+i*i,
b=j*j-i*i,
a=2*i*j;
if(1000==a+b+c)System.out.println("product: "+(a*b*c));
}
}
}

}

product: 31875000
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 2:20am On May 05, 2015
author=olyjosh post=33427863]

/**
*
* @author olyjosh
*/
public class PythagoreanTriplet {

public static void main(String[] args) {
int lim=1000/3;
for (int i = 2; i <=lim; i++) {
for (int j = i+1; j <= lim; j++) {
int c = j*j+i*i,
b=j*j-i*i,
a=2*i*j;
if(1000==a+b+c)System.out.println("product: "+(a*b*c));
}
}
}

}

product: 31875000[/quote]
Quite effective, check it out here http://ideone.com/Y3B3wP

Runtime: 0seconds
Memory; 0kb
Re: Trivia Coding Questions (euler Project) by kudaisi(m): 9:31am On May 05, 2015
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

(1) (2) (3) (4) (Reply)

Confused Between Node Js And PHP / Architectural Differences Between Virtual And Non-Virtual Machines / Convert Code From Php To Vb.net

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 77
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.