Skip to content

Commit c08193d

Browse files
Merge pull request matthewsamuel95#957 from psinha30/master
fibonacci using dp in c
2 parents 8e35ab3 + 2650647 commit c08193d

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

DP/Fibonacci/fibonacci_using_dp.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//Fibonacci Series using Dynamic Programming
2+
#include<stdio.h>
3+
4+
int fib(int n)
5+
{
6+
/* Declare an array to store Fibonacci numbers. */
7+
int f[n+2]; // 1 extra to handle case, n = 0
8+
int i;
9+
10+
/* 0th and 1st number of the series are 0 and 1*/
11+
f[0] = 0;
12+
f[1] = 1;
13+
14+
for (i = 2; i <= n; i++)
15+
{
16+
/* Add the previous 2 numbers in the series
17+
and store it */
18+
f[i] = f[i-1] + f[i-2];
19+
}
20+
21+
return f[n];
22+
}
23+
24+
int main ()
25+
{
26+
int n = 9;
27+
printf("%d", fib(n));
28+
getchar();
29+
return 0;
30+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
All submissions for this problem are available.Prime numbers are arranged in a ordered list U, in increasing order. Let S be a sublist of U with a unique property that for every element A belonging to list S, if i denotes the index of A in list U, than i also belongs to list U.
3+
4+
Given N, find sum of first N elements of list S, assuming 1-based indexing.
5+
6+
As the sum can be very large, print the sum modulo 109+7.
7+
8+
Input:
9+
-The first line of the input contains a single integer T denoting the number of test cases.
10+
11+
-Only line of each test case has an integer N .
12+
13+
Output:
14+
For each test case, print a single integer denoting the sum of first N elements of set S modulo 109+7.
15+
16+
Constraints
17+
1=T=10000
18+
1=N=1000*/
19+
#include<iostream>
20+
#include<vector>
21+
#include<cmath>
22+
using namespace std;
23+
int v[100000]={0};
24+
int su[100000]={0};
25+
int prime[100000];
26+
void calculate()
27+
{
28+
int i,j;
29+
int sum;
30+
v[0]=1;
31+
v[1]=1;
32+
su[0]=0;
33+
for(i=2;i<sqrt(100000);i++)
34+
if(v[i]==false)
35+
for (j = pow(i,2);j<=100000;j+=i)
36+
v[j] = true;
37+
sum=0;
38+
int k=0;
39+
prime[0] = 0;
40+
for(i=1;i<=100000;i++)
41+
{
42+
//king=0;
43+
if(v[i]==0)
44+
{
45+
k++;
46+
prime[k] = i;
47+
//cout<<i;
48+
}
49+
//v[]
50+
}
51+
int lo;
52+
su[0] = 0;
53+
lo=1;
54+
for(i=1;i<=k;i++)
55+
{
56+
if(v[i]==0)
57+
{
58+
sum = sum+prime[i];
59+
su[lo] = sum;
60+
lo++;
61+
}
62+
}
63+
}
64+
int main()
65+
{
66+
std::ios::sync_with_stdio(false);
67+
//cin.tie(0);
68+
69+
int t;
70+
long int n;
71+
calculate();
72+
cin>>t;
73+
74+
// vector<int> v;
75+
76+
while(t--)
77+
{
78+
cin>>n;
79+
// cout<<prime[n];
80+
81+
cout<<su[n]<<endl;
82+
//v.clear();
83+
}
84+
return 0;
85+
}

0 commit comments

Comments
 (0)