Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- int sieve(int a[],int n)
- {
- int position,temp,i;
- a[1]=1;
- for(position=3; position*position<=n; position=position+2)
- {
- for(temp=position; temp*position<=n; temp=temp+2)
- {
- a[temp*position]=1;
- }
- }
- for(i=4; i<=n; i=i+2) a[i]=1;
- return 0;
- }
- int main()
- {
- int n;
- scanf("%d",&n);
- int prime[n+1]= {0},position,temp,i,j;
- int sum[n+1]= {0};
- int index[n];
- for(i=1; i<=n; i++) index[i]=i;
- sieve(prime,n);
- //build up of sum array
- for(i=1; i<=n; i++)
- {
- if(prime[i]==0) //if the target index is prime
- {
- for(j=1; j<=i; j++)
- {
- if(prime[j]==1||j==i)
- sum[i]=sum[i]+index[j];
- else
- sum[i]=sum[i]+sum[j];
- }
- }
- else //if the target string is not prime
- {
- sum[i]=index[i];
- }
- }
- int total=0;
- for(i=1; i<n; i++)
- {
- total=total+sum[i];
- }
- printf("%d\n",total+n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement