Prime Palindrome Stops after a few iterations
Jul 3, 2014 at 6:38am UTC
I can't figure out why my code appears to stop after reaching 8. The output of my code is 1 2 3 for prime numbers and 4 6 8 for non primes (not sure where the 5 or 7 went either).
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
#include <iostream>
#include <vector>
using namespace std;
int main() {
int temp, d, revrs=0;
vector<int > v;
vector<int > w;
for (int i = 1; i < 1000; i++)
{
if (i == 2)
v.push_back(i);
else if (i == 3)
v.push_back(i);
else if ((i % 2 != 0) && (i % 3 != 0))
{
temp = i;
while (temp>0)
{
d = temp % 10;
temp /= 10;
revrs = revrs * 10 + d;
}
if (revrs == i)
{
v.push_back( i );
}
}
else
{
w.push_back(i);
}
}
for (int j = 0; j < v.size(); j++)
{
cout << v[j] << " " ;
}
cout << endl << endl << endl;
for (int j = 0; j < v.size(); j++)
{
cout << w[j] << " " ;
}
}
Last edited on Jul 3, 2014 at 6:38am UTC
Jul 3, 2014 at 8:53am UTC
1 2 3 4
for (int j = 0; j < v.size(); j++)
{
cout << w[j] << " " ;
}
i suppose it has to be w.size() to start, hasen't?
more,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
else if ((i % 2 != 0) && (i % 3 != 0))
{
temp = i;
while (temp>0)
{
d = temp % 10;
temp /= 10;
revrs = revrs * 10 + d;
}
if (revrs == i)
{
v.push_back( i );
}
}
here all the prime numbers that are not palindrome, go nowhere...
Last edited on Jul 3, 2014 at 12:10pm UTC
Topic archived. No new replies allowed.