Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,753 members, 7,817,083 topics. Date: Saturday, 04 May 2024 at 03:55 AM

Trivia Coding Questions (euler Project) - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Trivia Coding Questions (euler Project) (9909 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)

Trivia Coding Questions (euler Project) by kudaisi(m): 2:06pm On Apr 30, 2015
Series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

Have fun coding!!

PS: Try not to view other peoples solutions to problem until you have solved it yourself or you'll be deceiving yourself by copying other peoples answer, after all the whole idea it to improve your application logical thought patterns to programming.

Answer the questions in any language of your choice. Post both your solutions and the answer you arrived at after running the code.

1 Like

Re: Trivia Coding Questions (euler Project) by kudaisi(m): 2:10pm On Apr 30, 2015
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.

2 Likes

Re: Trivia Coding Questions (euler Project) by lordZOUGA(m): 2:27pm On Apr 30, 2015
sum(i for i in set( k for k in range(1, 1000) if (k % 3) == 0 or (k % 5) == 0))

answer = 233168
The weird/cool ass stuff python lets you do. smh.

1 Like

Re: Trivia Coding Questions (euler Project) by kudaisi(m): 2:42pm On Apr 30, 2015
#include <stdio.h>

int main (int argc, char argv){
int sum = 0;
int counter;
for(counter=0; counter < 1000; counter++){
if ((counter%3==0) || (counter%5==0)){
sum = sum + counter;
}
}
printf("The sum of all the multiples of 3 or 5 below 1000 is %d \n", sum);
}

Answer: 233168

1 Like

Re: Trivia Coding Questions (euler Project) by kudaisi(m): 2:43pm On Apr 30, 2015
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

1 Like 1 Share

Re: Trivia Coding Questions (euler Project) by kudaisi(m): 2:43pm On Apr 30, 2015
The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

1 Like

Re: Trivia Coding Questions (euler Project) by kudaisi(m): 2:43pm On Apr 30, 2015
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

1 Like

Re: Trivia Coding Questions (euler Project) by olyjosh(m): 8:56pm On Apr 30, 2015
Sum of even Fibonacci less than 4000000

public class FibonacciEven {

public static void main(String[] args) {
int num=4000000;
int fib=0;
int t2=2;
int t1=1;
int sum=2;
while (fib<num) {
fib=t1+t2;
t1=t2;t2=fib;
if(fib<num)if(fib%2==0)sum+=fib;
}
System.out.println(sum) ;
}

}


Answer : 4613732

1 Like

Re: Trivia Coding Questions (euler Project) by olyjosh(m): 10:40pm On Apr 30, 2015
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

2 Likes

Re: Trivia Coding Questions (euler Project) by tohero(m): 11:08pm On Apr 30, 2015
Intresting! Unfortunately there are limitations to my contributionsad

1 Like

Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:31pm On Apr 30, 2015
largest palindrome made from the product of two 3-digit numbers.

public class LargestPalindrome2_3 {
static boolean isPalindrome(String s){
return new StringBuffer(s).reverse().toString().equals(s) ;
}

public static void main(String[] args) {
int max = 999;
int min =100;
int prod=0;
boolean isTrue = false;
for (int i = max; i >=min ; i--) {
for (int j = max; j >= min; j--) {
prod=i*j;
if(isPalindrome(String.valueOf(prod))){
isTrue=true;
break;
}
}
if(isTrue)break;
}
System.out.println("largest palindrome made from the product of two 3-digit numbers is:"+prod) ;
}
}



largest palindrome made from the product of two 3-digit numbers is:580085

2 Likes

Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:34pm On Apr 30, 2015
tohero:
Intresting! Unfortunately there are limitations to my contributionsad

Limitations, How ?

1 Like

Re: Trivia Coding Questions (euler Project) by lordZOUGA(m): 9:43am On May 01, 2015
olyjosh:
largest palindrome made from the product of two 3-digit numbers.

public class LargestPalindrome2_3 {
static boolean isPalindrome(String s){
return new StringBuffer(s).reverse().toString().equals(s) ;
}

public static void main(String[] args) {
int max = 999;
int min =100;
int prod=0;
boolean isTrue = false;
for (int i = max; i >=min ; i--) {
for (int j = max; j >= min; j--) {
prod=i*j;
if(isPalindrome(String.valueOf(prod))){
isTrue=true;
break;
}
}
if(isTrue)break;
}
System.out.println("largest palindrome made from the product of two 3-digit numbers is:"+prod) ;
}
}



largest palindrome made from the product of two 3-digit numbers is:580085

How long did this code run?

1 Like

Re: Trivia Coding Questions (euler Project) by Nobody: 10:23am On May 01, 2015
this is my weak point.. nice one guys i intend to study more to improve my algorithm skills.

2 Likes

Re: Trivia Coding Questions (euler Project) by tohero(m): 11:39am On May 01, 2015
olyjosh:


Limitations, How ?
I can easily formulate the algorithm but writing them with a language is what I am yet to know
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:57am On May 01, 2015
lordZOUGA:


How long did this code run?

2 seconds on my 4gb RAM, 1.65GHz processor.
Any better solution without nested loop?
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 12:05pm On May 01, 2015
tohero:

I can easily formulate the algorithm but writing them with a language is what I am yet to know

Yeah, What language do you use in coding;

1 Like

Re: Trivia Coding Questions (euler Project) by olyjosh(m): 12:16pm On May 01, 2015
[quote author=olyjosh post=33308066]

Sorry it not upto a second
Logging Shows its under 20 milli-seconds
Re: Trivia Coding Questions (euler Project) by lordZOUGA(m): 1:53pm On May 01, 2015
olyjosh, okay. I just think it might be made faster if the loop iterated only palindromes between the product of 100 * 100 and 999 * 999. starting from the highest palindrome to the lowest palindrome.

this is assuming that the routine that generates the palindromes is fast
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 5:05pm On May 01, 2015
Yeah, that will make more sense if there is a way to generate palindromes first.
But my implementations generates product n test if it's palindrome from highest to lowest like you said.
Can u post a code or algorithmic representation so I can get you right.
Re: Trivia Coding Questions (euler Project) by tohero(m): 9:27pm On May 01, 2015
olyjosh:

Yeah, What language do you use in coding;
If u mean - will I b using, I wil say php, python nd java. Each at a time
I will b glad to hear ur opinion.
Re: Trivia Coding Questions (euler Project) by kudaisi(m): 9:46pm On May 01, 2015
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
Re: Trivia Coding Questions (euler Project) by kudaisi(m): 9:47pm On May 01, 2015
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.
Re: Trivia Coding Questions (euler Project) by kudaisi(m): 9:48pm On May 01, 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 olyjosh(m): 11:40pm On May 01, 2015
public class SmallestPositive {

public static void main(String[] arg){
int num, x = 20;
boolean isTrue=false;
for (num = x; !isTrue; num+=x) {
int i=0;
for (i = 2; i <x; i++) {
if(num%i!=0)break;
else if(i==x-1)isTrue=true;
}
if(isTrue)break;
}
System.out.println("smallest positive number that is evenly divisible by all of the numbers from 1 to 20 is "+num);
}

}


smallest positive number that is evenly divisible by all of the numbers from 1 to 20 is 232792560

1 Like

Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 12:01am On May 02, 2015
Let us park two spaces ...to post solutions later... :-)
- - - Forgive me, can't think now... :-)

EDIT 1: After solving some questions(below this post and next page), I thought I rearrange some workings into a cheatsheet.
Will update it when the need arise, or when time permits...
http://ideone.com/NqHgtM

1 Like

Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 12:16am On May 02, 2015
. . . .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....

1 Like

Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 12:43am On May 02, 2015
For the Fibonacci problem...
You may want to use some form of Dynamic Programming....

Example: ..(WARNING: Not tested!)... The member initialization is a C++11 language feature.

class Fibonacci {
std::vector<uint64_t> memory{0, 1};
public:
uint64_t compute(unsigned index){
if(index < memory.size())
return memory[index];
uint64_t c1 = compute(n - 1);
uint64_t c2 = compute(n - 2);
memory.push_back(c1 + c2);
return c1 + c2;
}
};

2 Likes

Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 1:18am On May 02, 2015
For Largest prime number, my blind guess goes this way...

For a number, say X;

1. Generate prime numbers with values up to or greater than X;, say P0, P1, P2 ... Pn
2. if Pn == X, then return Pn; ...else
3. You in in deep sh!t ....Combinatorics.... Here we come! :-) ...or
4. Actually, find the largest value of P that divides X completely... ...i.e X % P == 0;
5. let Y = X / P, find the next largest value from Pn that divides Y...

...silly, I know... Will try and update this and implement soon.
::Please bear with me kudasi, I will really love to try my hands on these things and refresh/resharpen my brain a bit. ...and most importantly, learn. ...So much on my hands, will modify my posts (transform them to my attempted solutions), hopefully before Monday.


@olyjosh.
I am really impressed! ...Would love to chat up with you Sir.
Re: Trivia Coding Questions (euler Project) by olyjosh(m): 3:10am On May 02, 2015
tohero:

If u mean - will I b using, I wil say php, python nd java. Each at a time

I will b glad to hear ur opinion.

I think you should go with any of C,C++,C#,python or java. Php is a script, I don't think it has such capability of doing trivial algorithmic problem like this. Someone should please correct me if I'm wrong
Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 3:21am On May 02, 2015
olyjosh:
largest palindrome made from the product of two 3-digit numbers.

    static boolean isPalindrome(String s){
return new StringBuffer(s).reverse().toString().equals(s) ;
}


Lovely implementation. Looks quadratic, but it works very well for small values of n; Kudus.
I think you should prefer StringBuilder instead of StringBuffer for this case... I am not much of a Java guy, so don't take my advice seriously. :-).

--> Ideone shows same time, but slightly less memory consumed, which quite frankly, is irrelevant (5KB) smiley .... http://ideone.com/PRx8qX and http://ideone.com/n4H8rL

1 Like

Re: Trivia Coding Questions (euler Project) by WhiZTiM(m): 3:22am On May 02, 2015
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.

Nonetheless...
Fibonacci Solution.... 4000000 max....

"""Very Fast reusable and Dynamic Fibonacci calculator """
class Fibonacci:
def __init__(self):
self.memory = [0, 1, 1]
def compute(self, n):
if n < len(self.memory):
return self.memory[n]
c1 = self.compute(n - 1)
c2 = self.compute(n - 2)
self.memory.append(c1 + c2)
return c1 + c2

def solve(limit):
fib = Fibonacci(); rtn = []
for x in range(limit):
n = fib.compute(x)
if n > limit: return rtn
if n % 2 == 0: rtn.append(n)

sum(solve(4000000))



Answer: 4613732
http://ideone.com/VSTUQe

smiley

....em off for the Day.

2 Likes

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

Create A Nested Lists Of Categories In Laravel 5 / Convert Code From Php To Vb.net / Architectural Differences Between Virtual And Non-Virtual Machines

(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. 44
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.