Calculating powers of 2
I am making a program to store all the powers of 2(1,2,4,8...) till 2 ^50000.
Although I am getting the answer,I am not getting my answer in the required time limit.
Any suggestions?
How should I store the numbers?
Here is my stupid code as of now
#include<stdio.h>
#include<math.h>
#include<string.h>
int bin[16604];
int main()
{
int n;
int t,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=0;i<16604;i++)
{
bin[i]=0;
}
bin[16603]=1;
//bin[4]=3;
int temp=0,x=0;
for( j=1;j<=n;j++)
{
for(i=16604;i>=0;i--)
{
temp=bin[i]*2+x;
bin[i]=temp%10;
x=temp/10;
// printf("temp %d x %d bin[i] %d\n",temp,x,bin[i]);
}
temp=0;x=0;
}
int j;
for( j=0;j<16604;j++)
if(bin[j])
break;
for( i=j;i<16604;i++)
{
printf("%d",bin[i]);
} //printf("%d",sum[n]);
putchar('\n');
}
return 0;
}
Here t is my number of test cases....
Here n is the number whose 2^n I am displaying.
Eg
2
10
1024
5
32
Related Article: read 2 digit numbers
is a C discussion thread by kylelendo that has 4 replies and was last updated 1 year ago.
swissknife007
Junior Poster in Training
75 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
My time limit is 5.5 seconds.
swissknife007
Junior Poster in Training
75 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
My time limit is 5.5 seconds.
Please any suggestion is highly welcomed...It means the world to me.
swissknife007
Junior Poster in Training
75 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
You know that your program can't reach that far because of the integer overflowing problem. Actually this program can be done within one for-loop.
Also, i think you can take away the second scanf() out of that while-loop, can you?
Where am i using integers to have an integer overflow,obviously u didn't understand the code it seems.
My code is displaying 2^50000 correctly.
The problem is with the efficiency part of it.
swissknife007
Junior Poster in Training
75 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0