Prime numbers
The program should calculate the sum of all the prime numbers greater or equal to the input number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
int prime(int n)
{
for(int i=2;i<n/2;i++) if(n%i==0) return 0;
return 1;
}
int sum(int arr[], int arrl)
{
if(arrl==0) return 0;
else return sum(arr, arrl - 1) + arr[arrl-1];
}
int main()
{
int index=0,arrl=0,arr[arrl-1]={},n,x;
cin>>n;
for(int j=2;j<=n;j++)
{
if(prim(j))
{
arrl++;
arr[index]=j;
index++;
}
}
x = sum(arr, arrl);
cout << x << endl;
getch();
}
|
The program should calculate the sum of all the prime numbers greater or equal to the input number |
Euclid has proved there are infinite prime numbers. This sum would diverge.
Topic archived. No new replies allowed.