6、
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.
前十个自然数的平方和是:
前十个自然数的和的平方是:
所以平方和与和的平方的差是3025 385 = 2640.
找出前一百个自然数的平方和与和平方的差。
#include#include #include #include #define N 100 int powplus(int n, int k){ int s=1; while(k--) { s*=n; } return s;} int sum1(int n){ return powplus((n+1)*n/2,2);} int sum2(int n){ return (n*(n+1)*(2*n+1))/6;} void solve(){ printf("%d\n",sum1(N)); printf("%d\n",sum2(N)); printf("%d\n",sum1(N)-sum2(N));} int main(){ solve(); return 0;}
7、
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?
前六个质数是2,3,5,7,11和13,其中第6个是13.
第10001个质数是多少?
#include#include #include #include int prim(int n){ int i; for(i=2; i*i<=n; i++) { if(n%i==0) return 0; } return 1;} void solve(int n){ int i=2; int count=0; while(1) { if(prim(i)) { count++; if(count==n) break; } i++; } printf("%d\n",i);} int main(){ int n=10001; solve(n); return 0;}
Completed on Thu, 4 Apr 2013, 17:34
8、
Find the greatest product of five consecutive digits in the 1000-digit number.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450找出以下这个1000位的整数中连续5个数字的最大乘积。(例如前五个数字的乘积是7*3*1*6*7=882)
#include#include int main(){FILE *fp;char *buffer;int i=0,j=0;fp=fopen("E://file.txt","r");char c;while((c=fgetc(fp))){ if(c==EOF) break; else if(c!='\n') i++;}buffer=(char *)malloc(i*sizeof(char));rewind(fp);while((c=fgetc(fp))){ if(c==EOF) break; else if(c!='\n') { *(buffer+j)=c; j++; }}findmax(buffer,i);}int findmax(char *buffer,int i){ int j=0,max=0; for(j=0;j
9、
A Pythagorean triplet is a set of three natural numbers, a b c, for which,
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.一个毕达哥拉斯三元组是一个包含三个自然数的集合,a<b<c,满足条件:
例如:32 + 42 = 9 + 16 = 25 = 52.
已知存在并且只存在一个毕达哥拉斯三元组满足条件a + b + c = 1000。
找出该三元组中abc的乘积。
#include#include #include #include #include #include void show(){ int a,b,c; for(a=1; a<333; a++) { for(c=333; c<500; c++) { b=1000-a-c; if(a*a+b*b==c*c) { printf("%d\n",a*b*c); return; } } }}int main(){ show(); return 0;}
10、
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
10以下的质数的和是2 + 3 + 5 + 7 = 17.
找出两百万以下所有质数的和。
#include#include #include #define N 2000000bool prim(int n){ int i; for(i=2; i*i<=n; i++) { if(n%i==0) return false; } return true;}int main(){ int i; long long sum=2; for(i=3; i<=N; i=i+2) { if(prim(i)) { sum+=i; } } printf("%lld\n",sum); return 0;}
Completed on Tue, 23 Jul 2013, 17:02