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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
int test(int x, int a[], int l)
{
int v = 0;
for(int i = 0; i < l; i++)
{
if(a[i] == x)
{
break;
v = 1;
}
}
return v;
}
int main()
{
cout << "Enter a number and I will generate the prime numbers up to that number: " << endl;
int a, d, i, j, num, cont = 1, N = 10000, l, p[N];
for(i = 1; i < N; i++)
{
p[i]=0;
}
cin >> num;
bool isPrime = true;
for(i = 2; i <= num; i++)
{
for(j = 2; j < sqrt(i) + 1; j++)
{
if( i % j == 0 )
{
break;
isPrime = false;
}
}
if(isPrime)
{
if(cont < N)
{
p[cont] = i;
cont = cont + 1;
}
cout << "p("<< cont - 1 << ")="<< p[cont] << endl
<< "Prime: " << i << endl;
}
isPrime = true;
}
cout << "Enter an int number l and I will generate an algebraic prime number sequences with length l."<< endl;
cin >> l;
d = 1;
for(i = 2; i < l; i++)
{
d = d * i;
}
cont = 1;
for(i = 1; i < 100; i++)
{
a = p[i];
while(test(a + d * cont, p, N) == 1)
{
cont = cont + 1;
}
if(cont > l - 1)
{
break;
}
}
cout << "Length of the algebraic sequence is: "<< cont << endl;
for(i = 0; i < cont; i++)
{
cout << "a("<< i + 1 << ")=" << a + i * d << endl;
}
cout << "Press ENTER to continue...";
cin.ignore(numeric_limits < streamsize > max(), "\n");
return 0;
}
|