Fibonacci and Primes

S ( N ) = F p \large S(N)=\sum F_p

Define the summation above for all primes p p less than N N .

The Fibonacci sequence { F n } \{F_n\} is defined by F 0 = 0 , F 1 = 1 F_0=0,F_1=1 and F n = F n 1 + F n 2 F_n=F_{n-1}+F_{n-2} for all integers n 2 n\geq 2 .

It is given that

S ( 10 ) = 21 S ( 1 0 2 ) 350775433 ( m o d 1 0 9 + 7 ) S ( 1 0 3 ) 347000363 ( m o d 1 0 9 + 7 ) \begin{aligned} S(10)&=&21 \\ S(10^2)&\equiv & 350775433\pmod{10^9+7} \\ S(10^3)& \equiv & 347000363\pmod{10^9+7} \end{aligned}

Find the value of S ( 1 0 7 ) S(10^7) modulo 1 0 9 + 7 10^9+7 .


The answer is 223971178.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Here is the code in C++. It runs in about 100-200 ms to get the answer 223971178 223971178 . We precompute the primes p p less than 1 0 7 10^7 and then evaluate F p \sum F_p .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
#define ll long long
#define N 10000000
#define mod 1000000007

bool arr[N+1];
ll primes[664580];
ll fib[N+1];

void init() {
    for(int i = 0; i < N+1; i++) { arr[i] = true; }
    arr[0] = arr[1] = false;
    for(int i = 2; i*i < N+1; i++) {
        if(arr[i]) {
            for(int j = 2; j*i < N+1; j++) {
                arr[j*i] = false;
            }
        }
    }
    primes[0] = 0;
    primes[1] = 2;
    int c = 2;
    for(int i = 3; i < N+1; i+=2) { if(arr[i]) primes[c++] = i; }
    fib[0] = 0;
    fib[1] = 1;
    for(int i = 2; i < N+1; i++) { fib[i] = (fib[i-1]+fib[i-2]) % mod; }
}

int main () {
    init();
    ll sum = 0;
    ll n =N;
    for(int i = 1; i < 664580; i++) {
        if(primes[i] <= n) { sum = (sum+fib[primes[i]]) % mod; }
    }
    printf("%lld\n", sum);
    return 0;
}

Rushikesh Jogdand
Jul 22, 2016
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
fib=[0,1]
for i in range(10**7-1):
    fib.append((fib[-1]+fib[-2])%int(10**9+7))          #taking mod 10**9+7 is the key step
su=0
su+=fib[2]+fib[3]
a,b=5,7
from gmpy import is_prime as p
while b<1000000:
    for i in [a,b]:
        if p(i):su+=fib[i]
    a+=6
    b+=6
print(su%(10**9+7))

Zawad Abdullah
Jun 19, 2015

I wrote a C code for this. It took almost 0.45 seconds to run.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<stdio.h>
#include<math.h>
int ara[10000001];
long int fib[10000001];
void sieve(void)
{
    int i, j, sq;
    for(i = 4; i < 10000001; i += 2)
    {
        ara[i] = 1;
    }
    sq = sqrt(10000001);
    for(i = 3; i <= sq; i += 2)
    {
        if(!ara[i])
        {
            for(j = i*i; j < 10000001; j += 2*i)
            {
                ara[j] = 1;
            }
        }
    }
}
long int fibo(void)
{
    int i;
    long int sum = 0;
    sieve();
    fib[0] = 0, fib[1] = 1;
    for(i = 2; i < 10000001; i++)
    {
        fib[i] = (fib[i-1] + fib[i-2])%1000000007;
        if(!ara[i])
        {
            sum = (sum + fib[i])%1000000007;
        }
    }
    return sum;
}
int main()
{
    int k;
    k = fibo();
    printf("%ld\n", k);
    return 0;
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...