Let k be a positive integer. Is it possible to find out the value of 3^k in mod 1000000007?
I have written a C program code to do this but it gives a message on the screen (signal: segmentation fault(core dumped)) if k>=1000000. My code:
#include <stdio.h>
#include <math.h>
#include<stdlib.h>
unsigned long long int fun(unsigned long long int x, unsigned long long
int L) {
if(x==0) return 1;
unsigned long long int y=(3*(fun(x-1,L)%L)%L + 1)%L;
return y;
}
int main()
{
unsigned long long int ans,B;
unsigned long long int L=pow(10,9)+7;
scanf("%llu",&B);
B=B%500000003;
if(B==0) {
ans=1;
} else {
ans=(2*fun(B-1,L)%L +1)%L;
}
printf("%llu\n",(ans)%L);
}
In this code, I have used a recursive function "fun(k-1,1000000007)" that gives the value of the sum y = 1+3+9+....+3^(k-1) in mod 1000000007. So the answer will be 2*y+1 (mod 1000000007) = 3^k(mod 1000000007).