init p1 to p9
This commit is contained in:
commit
8498165fef
9 changed files with 371 additions and 0 deletions
24
p1.c
Normal file
24
p1.c
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* http://projecteuler.net
|
||||||
|
*
|
||||||
|
* Projecteuler - Problem 1
|
||||||
|
* ------------------------
|
||||||
|
* 2008-01-21 Henrik Hautakoski
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define mod(x) ((x % 5) == 0 || (x % 3) == 0)
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int i, r = 0;
|
||||||
|
|
||||||
|
for(i=3; i < 1000; i++)
|
||||||
|
if (mod(i)) r += i;
|
||||||
|
|
||||||
|
printf("%i\n", r);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
29
p2.c
Normal file
29
p2.c
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* http://projecteuler.net
|
||||||
|
*
|
||||||
|
* Projecteuler - Problem 2
|
||||||
|
* -----------------------
|
||||||
|
* 2008-03-11 Henrik Hautakoski
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int c = 0, a = 1, b = 1, r = 0;
|
||||||
|
|
||||||
|
while(c < 4000000) {
|
||||||
|
|
||||||
|
c = a+b;
|
||||||
|
a = b;
|
||||||
|
b = c;
|
||||||
|
|
||||||
|
if((c % 2) == 0)
|
||||||
|
r += c;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%i\n", r);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
44
p3.c
Normal file
44
p3.c
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* http://projecteuler.net
|
||||||
|
*
|
||||||
|
* Projecteuler - Problem 3
|
||||||
|
* ------------------------
|
||||||
|
* 10/01/10 (binary day :D) Henrik Hautakoski
|
||||||
|
*
|
||||||
|
* fast solution using euclidean algorithm for calculating gcd.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef unsigned long bint;
|
||||||
|
|
||||||
|
bint gcd(bint a, bint b) {
|
||||||
|
|
||||||
|
bint c;
|
||||||
|
|
||||||
|
while(b != 0) {
|
||||||
|
c = b;
|
||||||
|
b = a % b;
|
||||||
|
a = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
bint i, r = 0, n = 600851475143;
|
||||||
|
|
||||||
|
for(i=2; i <= n ; i++) {
|
||||||
|
|
||||||
|
if((r = gcd(n, i)) == 1)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
n /= r;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%li\n", r);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
58
p4.c
Normal file
58
p4.c
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* http://projecteuler.net
|
||||||
|
*
|
||||||
|
* Projecteuler - Problem 4
|
||||||
|
* ------------------------
|
||||||
|
* 2009-12-28 Henrik Hautakoski
|
||||||
|
*
|
||||||
|
* A somewhat elegant solution (representing the numbers as a string is not so fancy)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int factor(int val) {
|
||||||
|
|
||||||
|
int a;
|
||||||
|
|
||||||
|
for(a=999; a > 99; a--) {
|
||||||
|
|
||||||
|
if((val % a) == 0 && (val/a) < 999)
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ispalindrom(int num) {
|
||||||
|
|
||||||
|
int i, n;
|
||||||
|
char buffer[7];
|
||||||
|
|
||||||
|
sprintf(buffer, "%i", num);
|
||||||
|
for(n=0; buffer[n+1] != 0; n++);
|
||||||
|
|
||||||
|
for(i=0; i < n-i; i++) {
|
||||||
|
|
||||||
|
if (buffer[i] != buffer[n-i])
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int r, a;
|
||||||
|
|
||||||
|
for(r=999999; r >= 100001; r--) {
|
||||||
|
|
||||||
|
if (!ispalindrom(r) || !(a = factor(r)))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
printf("%i * %i = %i\n", r/a, a, r);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
31
p5.c
Normal file
31
p5.c
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* http://projecteuler.net
|
||||||
|
*
|
||||||
|
* Projecteuler - Problem 5
|
||||||
|
* ------------------------
|
||||||
|
* 2008-03-11 Henrik Hautakoski
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int i, n = 20;
|
||||||
|
char r;
|
||||||
|
|
||||||
|
do {
|
||||||
|
r = 0;
|
||||||
|
for(i=20; i > 10; i--) {
|
||||||
|
if ((n % i) != 0) {
|
||||||
|
n += 20;
|
||||||
|
r = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while(r);
|
||||||
|
|
||||||
|
printf("%i\n", n);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
22
p6.c
Normal file
22
p6.c
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* http://projecteuler.net
|
||||||
|
*
|
||||||
|
* Projecteuler - Problem 6
|
||||||
|
* ------------------------
|
||||||
|
* 2008-03-13 Henrik Hautakoski
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int i, s1 = 1, s2 = 1;
|
||||||
|
|
||||||
|
for(i=2; i <= 100; i++) {
|
||||||
|
s1 += i*i;
|
||||||
|
s2 += i;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%i\n", s2*s2 - s1);
|
||||||
|
}
|
||||||
55
p7.c
Normal file
55
p7.c
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* http://projecteuler.net
|
||||||
|
*
|
||||||
|
* Projecteuler - Problem 7
|
||||||
|
* ------------------------
|
||||||
|
* 2009-12-28 Henrik Hautakoski
|
||||||
|
*
|
||||||
|
* fast and elegant solution using The fundamental theorem of arithmetic wich state:
|
||||||
|
* any natural number greater than 1 can be prime factorized in only one unique way
|
||||||
|
* unless it is prime itself.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
|
||||||
|
int isprime(int *pl, int l, int p) {
|
||||||
|
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for(i=0; i < l; i++) {
|
||||||
|
|
||||||
|
if((p % pl[i]) == 0)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int i, len = 3, *prime = malloc(sizeof(int)*10001);
|
||||||
|
|
||||||
|
if (prime == NULL)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
prime[0] = 2;
|
||||||
|
prime[1] = 3;
|
||||||
|
prime[2] = 5;
|
||||||
|
|
||||||
|
for(i=6; len < 10001; i++) {
|
||||||
|
|
||||||
|
if(!isprime(prime, len, i))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
prime[len++] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%i\n", prime[len-1]);
|
||||||
|
|
||||||
|
free(prime);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
65
p8.c
Normal file
65
p8.c
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* http://projecteuler.net
|
||||||
|
*
|
||||||
|
* Projecteuler - Problem 8
|
||||||
|
* ------------------------
|
||||||
|
* 2009-12-28 Henrik Hautakoski
|
||||||
|
*
|
||||||
|
* No comment!
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define N_CHARS 1000
|
||||||
|
|
||||||
|
#define BIGWALLOFTEXT \
|
||||||
|
"73167176531330624919225119674426574742355349194934\
|
||||||
|
96983520312774506326239578318016984801869478851843\
|
||||||
|
85861560789112949495459501737958331952853208805511\
|
||||||
|
12540698747158523863050715693290963295227443043557\
|
||||||
|
66896648950445244523161731856403098711121722383113\
|
||||||
|
62229893423380308135336276614282806444486645238749\
|
||||||
|
30358907296290491560440772390713810515859307960866\
|
||||||
|
70172427121883998797908792274921901699720888093776\
|
||||||
|
65727333001053367881220235421809751254540594752243\
|
||||||
|
52584907711670556013604839586446706324415722155397\
|
||||||
|
53697817977846174064955149290862569321978468622482\
|
||||||
|
83972241375657056057490261407972968652414535100474\
|
||||||
|
82166370484403199890008895243450658541227588666881\
|
||||||
|
16427171479924442928230863465674813919123162824586\
|
||||||
|
17866458359124566529476545682848912883142607690042\
|
||||||
|
24219022671055626321111109370544217506941658960408\
|
||||||
|
07198403850962455444362981230987879927244284909188\
|
||||||
|
84580156166097919133875499200524063689912560717606\
|
||||||
|
05886116467109405077541002256983155200055935729725\
|
||||||
|
71636269561882670428252483600823257530420752963450"
|
||||||
|
|
||||||
|
int product(char *ptr, int n) {
|
||||||
|
|
||||||
|
int i, r = 1;
|
||||||
|
|
||||||
|
for(i=0; i < n; i++)
|
||||||
|
r *= ((int)ptr[i]) - 0x30;
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int i;
|
||||||
|
char str[] = BIGWALLOFTEXT;
|
||||||
|
int tmp, r = 0;
|
||||||
|
|
||||||
|
for(i=0; i < N_CHARS-5; i++) {
|
||||||
|
|
||||||
|
tmp = product(&str[i], 5);
|
||||||
|
|
||||||
|
if (tmp > r)
|
||||||
|
r = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%i\n", r);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
43
p9.c
Normal file
43
p9.c
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* http://projecteuler.net
|
||||||
|
*
|
||||||
|
* Projecteuler - Problem 9
|
||||||
|
* ------------------------
|
||||||
|
* 2010-04-06 Henrik Hautakoski
|
||||||
|
*
|
||||||
|
* the math stuff here (simple algebra)
|
||||||
|
*
|
||||||
|
* 1000 = a + b + c
|
||||||
|
* 10^6 = (a + b + c)^2
|
||||||
|
* 10^6 = a^2 + ab + ac + ba + b^2 + bc + ca + cb + c^2
|
||||||
|
* 10^6 = a^2 + b^2 + c^2 + 2(ab + ac + bc)
|
||||||
|
* 10^6 = 2a^2 + 2b^2 + 2(ab + ac + bc) (substitution: a^2 + b^2 = c^2, pythagorean theorem)
|
||||||
|
* 10^6/2 = a^2 + b^2 + ab + ac + bc
|
||||||
|
* 10^6/2 + ab = a^2 + b^2 + 2ab + ac + bc
|
||||||
|
* 10^6/2 + ab = (a + b)(a + b) + (a + b)c
|
||||||
|
* 10^6/2 + ab = (a + b + c)(a + b)
|
||||||
|
* 10^6/2 + ab = 1000a + 1000b (substitution: a + b + c = 1000)
|
||||||
|
* 10^6/2 = 1000a + 1000b - ab
|
||||||
|
* 10^6/2 = a(1000 - b) + 1000b
|
||||||
|
*
|
||||||
|
* Equation to use: a = (10^6/2 - 1000b)/(1000 - b)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int a, b;
|
||||||
|
double tmp;
|
||||||
|
|
||||||
|
for(b=1; b < 1000; b++) {
|
||||||
|
a = tmp = (5e5 - 1000*b)/(1000 - b);
|
||||||
|
if (tmp - a >= 0.0 && (tmp - a) < .00000001) {
|
||||||
|
printf("%i\n", a*b*(1000 - (a+b)));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue