Amazing Prime Sequence
given below c code for aps spoj or amazing prime sequence.
If you have any quarry you can mail me @ raj.nishant360@gmail.com
#include <bits/stdc++.h> int p[10000009]; long long res[10000009]; void pre() { for(int i=2;i<=10000000;i++) { if(!p[i]) { for(int j=i+i;j<=10000000;j+=i) if(!p[j]) p[j]=i; res[i]=res[i-1]+i; } else res[i]=res[i-1]+p[i]; } } int main() { int t; pre(); scanf("%d",&t); while(t--) { int n; scanf("%d",&n); printf("%lld\n",res[n]); } return 0; }
#include
ReplyDeleteusing namespace std;
int p[100];
int res[100];
int prime(int n){
res[0]=0;
res[1]=0;
p[0]=0;
p[1]=1;
for(int i=2;i<=n;i++){
if(p[i]==0){
p[i]=i;
//res[i]=res[i-1]+i;
int j=2;
while(i*j<=n){
if(p[i*j]==0){
p[i*j]=i;
res[i]=res[i-1]+i;
}
j++;
}
}
else{
res[i]=res[i-1]+p[i];
}
}
return res[n];
}
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
cout<<prime(n)<<endl;
}
}
please tell why it is not working in third test case when n=4 it is giving output 9.
please help