# creasyw/Courses

### Subversion checkout URL

You can clone with
or
.

Project Euler practice

commit 610bb12ffac0ec88a3e1b57ff01ba71da7b7e63b 1 parent 8cf37c9
authored
Showing with 303 additions and 0 deletions.
1. +248 −0 puzzles/q001To005.c
2. +55 −0 puzzles/q006To010.c
3. puzzles/test
248 puzzles/q001To005.c
 @@ -0,0 +1,248 @@ +#include +#include +#include + +// for question3,4 +#define MAX(a,b) (a>b?a:b) + + +int MaxInt(int a, int b) +{ + if(a>=b) + return a; + else + return b; +} + +// 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. +// SOLUTION: Brutal force might be a straight forward solution:) +int question1() +{ + int sum = 0; + int i; + for( i=1; i<1000; i++) + { + if(i%3==0 || i%5==0) + sum +=i; + } + return sum; +} + +// 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. +// SOLUTION: The fibonacci sequence should be: odd, even, odd, odd, even, odd, odd, even... +// So, the sum should contains elements with index 2,5,8,11...... +int question2() +{ + int sum=2; + int fab[3]={1,2,3}; // storing the fab sequence and only add the middle to "sum" + int index; + int count = 3; + while(1) + { + index=count%3; + fab[index]=fab[(count+1)%3]+fab[(count+2)%3]; + if(fab[index]>4000000) + break; + count++; + if(index==1) + sum +=fab[index]; + } + return sum; +} + + +// The prime factors of 13195 are 5, 7, 13 and 29. +// What is the largest prime factor of the number 600851475143 ? +// SOLUTION: There are quite a few algorithms to solve in in wiki: http://en.wikipedia.org/wiki/Integer_factorization +// However, most of them are targetting at HUGE numbers with "100 digits". +// So, considering the algorithm complexity and the input number in this puzzle, +// it might be a good trade-off to considering 3 to n^.5 without even number. +// The input for this question is: question3(3,600851475143); +// The "begin" is just a small optimization cutting running time by 67%. +long question3(long begin, long input) +{ + //int input = 600851475143; + long square; + long result = 0; + long i; + + if(input==1) + return 1; + while(input%2==0) + input = input/2; + square = ceil(sqrt(input)); + for(i=begin;i=100) + { + if(a%11==0) + { + b=999; + db=1; + } else { + b=990; + db=11; + } + while(b>=a) + { + product=a*b; + if(product<=result) + break; + // test if a*b is palindrome + test1 = (int)product/100000; + if(test1==product%10) + { + test2=((int)product/10000)%10; + if(test2==((int)product/10)%10) + { + test3=((int)product/1000)%10; + if(test3==((int)product/100)%10) + { + result=product; + break; + } + } + } + b=b-db; + } + a--; + } + return result; +} + + +// 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? +// SOLUTION: the array can be divided as 2-n^.5 and n^.5-n. For the former part, we need to know how many powers for each prime, and for the latter part, we just need to multiply all of the primes. +int question5() +{ + int range = 20; + int square = ceil(sqrt(range)); + int n, power; + int result = 1; + int i, flag=0; + + // find the power from 2 to square + for(n=2;n=range) + break; + power++; + } + result = result*pow(n,(power-1)); + } + + // find all of the primes within the range + for(n=square;n
55 puzzles/q006To010.c
 @@ -0,0 +1,55 @@ +#include +#include +#include + +// 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. +// SOLUTION: This can be solved with the formula that calculate the sum of consecutive natural sequence: 1^2+2^2+...+n^2=n(n+1)(2n+1)/6, and the sum of consecutive natural numbers: 1+2+...+n=(n+1)n/2 +int question6() +{ + int n = 100; + int sum1, sum2; + + sum1 = pow(n*(n+1)/2, 2); + sum2 = n*(n+1)*(2*n+1)/6; + return sum1-sum2; +} + +// 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 10001st prime number? +int question7() +{ + int count = 6; + int n=17; + int i; + int flag=0; + + while(count!=10001) + { + for(i=2;i<=floor(sqrt(n));i++) + { + if(n%i==0) + { + flag=1; + break; + } + } + if(flag) + flag=0; + else{ + count++; + printf("count=%d\tn=%d\n",count,n); + } + n+=2; + } + return n-2; // the last round add 2 more to the final result +} + + +int main() +{ + printf("result = %d\n", question7()); + return 0; +} + +
BIN  puzzles/test
Binary file not shown