Problem: Find the number of integers 1 < n < 10^7, for which n and n + 1 have the same number of positive divisors. For example, 14 has the positive divisors 1, 2, 7, 14 while 15 has 1, 3, 5, 15.
I can’t reach 10^7 because it is too big number for C and me. How can i solve this problem in C?
#include<stdio.h>
#include<conio.h>
int divisorcount(int);
int main()
{
int number,divisornumber1,divisornumber2,j=0;
for(number=1;number<=100;number++){
divisornumber1=divisorcount(number);
divisornumber2=divisorcount(number-1);
if(divisornumber1==divisornumber2){
printf("%d and %d\n",number-1,number);
j++;
}
}
printf("\nThere is %d integers.",j);
getch();
}
int divisorcount(int num)
{
int i,divi=0;
for(i=1;i<=(num)/2;i++)
if(num%i==0)
divi++;
return divi;
}
Ever tried
long long num = 100000000LL;? C isn’t smart enough to conclude the type on the right side from the leftlong longso you have to add theLL. With this approach you should be able to handle larger numbers than normal integers, just change your functions and variables in a suitable way.A
long longis always at least2^64bit in size which you can check on Wikipedia.Hint: As someone mentioned in the comments, Project Euler is not about bruteforcing. This is a lame approach. Think about some better strategies. You might want to get help at math.stackexchange?
EDIT: I don’t know why I thought, that a
uint32_tis not enough for10^7– sorry for that mistake.