Having Diffculty with my emirp code

As you can see, i have a reversal function in my code. but getting it to work with my prime numbers that i loop through is having me confused. I'm avoiding from the use of an array so i cant store my prime numbers into arrays. Any tip would be great!

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
  #include <iostream>
#include <iomanip>

using namespace std;
int reversal(int num);

int main()
{
    int num =1;
    int space = 0;
        while (space < 100)
		{
            num++;
            if ((num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0) || (num % 7 == 0))
			{

			}

			else
			{ 

                reversal(num);


                cout << setw(5) << num;
                space++;
                    if (space % 10 == 0)
					{
                        cout << endl;
					}



			}
            
		}
        system("pause");
        return 0;
}

int reversal(int num)
{
        int reverse = 0;
	while (num != 0)
	     {
	    reverse = reverse * 10;
	    reverse = reverse + (num % 10);
	     num = num / 10;
	     }
    return reverse;
}
I don't really understand what your code is trying to do. First of all, I notice that your reversal function is being used - you call it and it returns, but you don't store the return value anywhere.

What are the numbers you're trying to print? They are mostly prime numbers, but it also includes things like 11*11 (121) and 11*13 (143) which isn't prime. Is that the problem? You didn't actually describe your problem, or the expected / intended results, so I don't know.
The numbers generated are not all prime. Cause you are testing it with only 3,5 and 7... You need to test it with any new prime in the sequence.


This will generate all primes between 2 given numbers:
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
#include <iostream>
#include <vector>


using std::cout;
using std::cin;
using std::endl;
using std::vector;

int main(int argc, char** argv) {
    
    int howmany=0;
    
    int first, scnd;
    int total;
    
    //cin >> howmany;
    cout<<"Enter 2 numbers (x to y)";
    cin >> first >> scnd;
    if(first==1)first++;
    total = (scnd - first) +1;
    
    
    
    
    vector<int> vec(total+1);
    vector<int> vec2(total+1);
    
    vec[0] = first;
    vec2[0] = first;
    for(int i = 1; i<= total; ++i){
        
        vec[i] = first + i;
        vec2[i] = first + i;
        
    
    }
    
    
    for(int i=0; i< total; ++i){
        
    
        for(int k = total -1 ; k >=0; --k){
            
            //cout << endl << "j: " << vec2[k] << " i: "<< vec[i];
            
            if(vec2[k] % vec[i] == 0 && vec2[k] != vec[i] && vec2[k] > vec[i]){ 
                
                vec2[k] = -1;
            }
            
            
            
        }
        
    }
    
    
    
    

    cout << "Primes: ";
    for(int i = 0; i< total; ++i){
        if(vec2[i]!=-1)cout << vec2[i] << " ";
        
    }
    
    
}


this code needs optimization...
Last edited on
Topic archived. No new replies allowed.