firstly, please suggest some tags i should put for this post.
onto the problem..
i saw this programming problem on projecteuler.net and wanted to solve it for fun.
i solved it, but i tried first till 142857
the output was good, ok, satisfying
here's the link where the problem is posted.
http://projecteuler.net/index.php?section=problems&id=168
but as the problem specifies, we have to calculate the sum of all those kind of numbers, and display the last 5 digits of that sum. now i don't know if that can be done quicker mathematically using some reductions are series stuff. my solution is taking up like around 2minutes to output the next number having that property after it reaches upto 111111111 (9 times 1)
and it stopped at 10 times 1 (1111111111)
edit: i forgot to declare the paramater of the getNumberLength() function as unsigned long long.
it might go beyond 10x 1s i guess then
so how do i make it faster? some high level maths to reduce the prediction of sum's last 5 digits to a fewer iterations or do i need some compiler optimization..
hell i don't even know what i'm talking about xD when it comes to optimizations.
i have a 32 bit core2duo intel p8600 processor,if that might help in any thing for explaining the slowing down of the execution
i know i'm a lousy programmer, i use many unnecessary variable declarations,iterations, inefficient loops etc. my friends tell me that all the time. but i'm only learning
here's the code
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int getNumberLength(unsigned long long n)
{
int c=0;
while(n>0)
{
n/=10;
c++;
}
return c;
}
int main()
{
unsigned long long i = 10;
unsigned long long temp;
unsigned long long j=(long)pow(i,100);
cout<<"J="<<j<<endl;
unsigned long long sum=0;
while(i<=j)//upto 142857 it was fine, normally fast. when set to j, time went upto //like 11 minutes i guess
{
temp=((i%10)*(pow(10,getNumberLength(i)-1))+(i/10));
if(temp%i==0)
{cout<<i<<"\t"<<temp<<endl;
sum+=i;
}
i++;
}
cout<<"\n\n Sum:"<<sum;
cout<<"\n\n last five digits:\t";
temp=sum;i=1;
while(sum>10)
{
sum/=10;i*=10;
}
cout<<(temp-(sum*i));
getch();
return EXIT_SUCCESS;
}