Having Trouble with C++ Checker

Hi people,

I have problems with the following code. It is supposed to expand on the green tao theorem.

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Efficent Prime Check 1.cpp : Defines the entry point for the console application.
//Created by Sutton Shin
//(C) Sutton Shin
//Written in the C++ Language
//   \\\ ||||///
//   |//   // |
//   |0     0 |
//   |-      -|
//     ------
//      |  |
//      /  \
//     /    \
//    |  C++ |
//    |  Guy |
//    |      |
//    --------
//     ||  ||
//	   --  --
//     |_* |_*

#include "stdafx.h"


#include <iostream> 
#include <math.h>
//test whether a number x appears in a sequence p[0], p[1]...p[l].
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()
{
    
    std::cout << " Enter a number and I will generate the prime numbers up to that number: "<<std::endl;
    int a,d,i,num, cont= 1, N=10000,l; int p[N];
    for (i=1;i<N;i++ )
    {
        p[i]=0;
    }
    std::cin >> num;
    bool isPrime=true;
    for ( int i = 2; i <= num; i++)
    {
        for ( int j = 2; j <sqrt(i)+1; j++)
        {
            if ( i % j == 0 )
            {
                isPrime=false;
                break;
            }
        }
        if (isPrime)
        {
            if (cont<N){
                p[cont]=i;
                cont=cont+1;
            }
            std::cout <<"p("<< cont-1 <<")="<<p[cont]<< std::endl;

            std::cout <<"Prime:"<< i << std::endl;
        }
        isPrime=true;
    }
// write down the algebraic sequence
    std::cout << " Enter an int number l and I will generate an algebraic prime number sequences with length l "<<std::endl;
    std::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;
        }
    }
    std::cout <<"length of the algebraic sequence is:"<< cont << std::endl;
    for (i=0;i<cont;i++)
    {
        std::cout <<"a("<< i+1 <<")="<<a+i*d << std::endl;

    }

  std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    
    
    return 0;
}




The code does not even execute as required, and i cannot find out what is wrong.
Last edited on
closed account (ShpjE3v7)
Try it:
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;
}
Last edited on
it still does not work.
Topic archived. No new replies allowed.