Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. bool isNPrime(int n) {
  7. bool isPrime = true;
  8. for(int i = 2; i <= n / 2; ++i)
  9. {
  10. if(n % i == 0)
  11. {
  12. isPrime = false;
  13. break;
  14. }
  15. }
  16. return isPrime;
  17. }
  18.  
  19. float fib(int n) {
  20. int nlen = n*2, i = 0, cnt = 0;
  21. float sum = 0;
  22. float t1 = 1, t2 = 1, next = 0;
  23.  
  24. do {
  25.  
  26. if(i == 0 || i == 1)
  27. {
  28. sum += 1.0f;
  29. cnt++;
  30. // cout << endl << sum << " " << i;
  31. i++;
  32. continue;
  33. }
  34. next = t1 + t2;
  35. t1 = t2;
  36. t2 = next;
  37.  
  38. sum += next;
  39. cnt++;
  40. i++;
  41. } while(cnt < nlen);
  42. // cout << endl << sum << " " << cnt;
  43. float res = sum / nlen;
  44. // cout << res;
  45. return res;
  46. }
  47.  
  48. int prosti(int n) {
  49. int nlen = n*n;
  50. int pos = 0, sum = 0;
  51. int num = 2;
  52. while( pos < nlen) {
  53. bool isPrime = isNPrime(num);
  54. if(isPrime) {
  55. sum += num;
  56. pos++;
  57. num++;
  58. // cout << num << " ";
  59. } else {
  60. num++;
  61. }
  62. }
  63. return sum;
  64. }
  65.  
  66.  
  67. int main() {
  68.  
  69. int n;
  70. cin >> n;
  71.  
  72. float sr_arit = fib(n);
  73. float sum_prosti = prosti(n);
  74.  
  75. // cout << endl << sr_arit << " " << sum_prosti << endl;
  76. // cout << endl << sum_prosti;
  77. float sum = sr_arit * sum_prosti;
  78. cout << sum;
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement