Advertisement
mahmud11556

Untitled

Feb 22nd, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int sieve(int a[],int n)
  5. {
  6. int position,temp,i;
  7. a[1]=1;
  8. for(position=3; position*position<=n; position=position+2)
  9. {
  10. for(temp=position; temp*position<=n; temp=temp+2)
  11. {
  12. a[temp*position]=1;
  13. }
  14. }
  15. for(i=4; i<=n; i=i+2) a[i]=1;
  16. return 0;
  17. }
  18.  
  19.  
  20.  
  21. int main()
  22. {
  23. int n;
  24. scanf("%d",&n);
  25.  
  26. int prime[n+1]= {0},position,temp,i,j;
  27. int sum[n+1]= {0};
  28. int index[n];
  29. for(i=1; i<=n; i++) index[i]=i;
  30.  
  31. sieve(prime,n);
  32.  
  33. //build up of sum array
  34. for(i=1; i<=n; i++)
  35. {
  36. if(prime[i]==0) //if the target index is prime
  37. {
  38. for(j=1; j<=i; j++)
  39. {
  40. if(prime[j]==1||j==i)
  41. sum[i]=sum[i]+index[j];
  42. else
  43. sum[i]=sum[i]+sum[j];
  44. }
  45.  
  46. }
  47. else //if the target string is not prime
  48. {
  49. sum[i]=index[i];
  50. }
  51.  
  52. }
  53.  
  54.  
  55. int total=0;
  56. for(i=1; i<n; i++)
  57. {
  58. total=total+sum[i];
  59. }
  60.  
  61. printf("%d\n",total+n);
  62. return 0;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement