parent
061108eb02
commit
43949f66df
Binary file not shown.
Binary file not shown.
@ -0,0 +1,55 @@ |
|||||||
|
#include <stdio.h> |
||||||
|
|
||||||
|
/*
|
||||||
|
The prime factors of 13195 are 5, 7, 13 and 29. |
||||||
|
|
||||||
|
What is the largest prime factor of the number 600851475143 ? |
||||||
|
|
||||||
|
https://projecteuler.net/problem=3
|
||||||
|
*/ |
||||||
|
int main(int argc,char**argv) { |
||||||
|
|
||||||
|
long primes[10000]; |
||||||
|
int primeCount=1; |
||||||
|
long primeVal=3; |
||||||
|
long highestPrime=1; |
||||||
|
primes[0]=2; |
||||||
|
long startingNumb=600851475143; |
||||||
|
label: |
||||||
|
while (startingNumb>1) { |
||||||
|
//find the next prime.
|
||||||
|
char isPrime=1; |
||||||
|
for (int i=0;i<primeCount;i++) { |
||||||
|
//first try all current primes.
|
||||||
|
if (startingNumb%primes[i]==0) { |
||||||
|
//It's divisible!
|
||||||
|
startingNumb/=primes[i]; |
||||||
|
printf(" Factor: %ld\n",primes[i]); |
||||||
|
if (highestPrime<primes[i]) { |
||||||
|
highestPrime=primes[i]; |
||||||
|
} |
||||||
|
goto label; |
||||||
|
} |
||||||
|
if (primeVal%primes[i]==0) { |
||||||
|
isPrime=0; |
||||||
|
} |
||||||
|
} |
||||||
|
if (isPrime) { |
||||||
|
primes[primeCount++]=primeVal; |
||||||
|
//printf("Generated a new prime: %ld\n",primeVal);
|
||||||
|
if (startingNumb%primeVal==0) { |
||||||
|
//It's divisible!
|
||||||
|
startingNumb/=primeVal; |
||||||
|
printf(" Factor: %ld\n",primeVal); |
||||||
|
if (highestPrime<primeVal) { |
||||||
|
highestPrime=primeVal; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
primeVal++; |
||||||
|
} |
||||||
|
|
||||||
|
printf("Highest prime is %ld",highestPrime); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
@ -1,30 +1,55 @@ |
|||||||
#include <stdio.h> |
#include <stdio.h> |
||||||
|
|
||||||
/*
|
/*
|
||||||
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: |
The prime factors of 13195 are 5, 7, 13 and 29. |
||||||
|
|
||||||
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... |
What is the largest prime factor of the number 600851475143 ? |
||||||
|
|
||||||
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. |
https://projecteuler.net/problem=3
|
||||||
|
|
||||||
https://projecteuler.net/problem=2
|
|
||||||
*/ |
*/ |
||||||
int main(int argc,char**argv) { |
int main(int argc,char**argv) { |
||||||
int fibStorage[1000]; |
|
||||||
int val=0; |
long primes[10000]; |
||||||
int counter=0; |
int primeCount=1; |
||||||
int sum=0; |
long primeVal=3; |
||||||
while (val<4000000) { |
long highestPrime=1; |
||||||
if (counter>2) { |
primes[0]=2; |
||||||
val=fibStorage[counter++]=fibStorage[counter-2]+fibStorage[counter-1]; |
long startingNumb=600851475143; |
||||||
} else { |
label: |
||||||
val=fibStorage[counter++]=counter; |
while (startingNumb>1) { |
||||||
|
//find the next prime.
|
||||||
|
char isPrime=1; |
||||||
|
for (int i=0;i<primeCount;i++) { |
||||||
|
//first try all current primes.
|
||||||
|
if (startingNumb%primes[i]==0) { |
||||||
|
//It's divisible!
|
||||||
|
startingNumb/=primes[i]; |
||||||
|
printf(" Factor: %ld\n",primes[i]); |
||||||
|
if (highestPrime<primes[i]) { |
||||||
|
highestPrime=primes[i]; |
||||||
|
} |
||||||
|
goto label; |
||||||
|
} |
||||||
|
if (primeVal%primes[i]==0) { |
||||||
|
isPrime=0; |
||||||
|
} |
||||||
} |
} |
||||||
printf("Fib %d is %d\n",counter,val); |
if (isPrime) { |
||||||
if ((val&1)==0) { |
primes[primeCount++]=primeVal; |
||||||
sum+=val; |
//printf("Generated a new prime: %ld\n",primeVal);
|
||||||
|
if (startingNumb%primeVal==0) { |
||||||
|
//It's divisible!
|
||||||
|
startingNumb/=primeVal; |
||||||
|
printf(" Factor: %ld\n",primeVal); |
||||||
|
if (highestPrime<primeVal) { |
||||||
|
highestPrime=primeVal; |
||||||
|
} |
||||||
|
} |
||||||
} |
} |
||||||
|
primeVal++; |
||||||
} |
} |
||||||
printf("Sum: %d",sum); |
|
||||||
|
printf("Highest prime is %ld",highestPrime); |
||||||
|
|
||||||
return 0; |
return 0; |
||||||
} |
} |
Loading…
Reference in new issue