emirps

Mar 4, 2008 at 6:03pm
I need help on the source code for the first 100 emirps. Phelp!!
Last edited on Mar 4, 2008 at 6:04pm
Mar 6, 2008 at 12:45am
This might help.
http://www.lhs.logan.k12.ut.us/~rweeks/cp2/emirp.htm

Write what you can and if you still have trouble, post back with what you did and we'll help.
Mar 7, 2008 at 7:24pm
This is my code up til' now but a'm not getting any further.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
// variables
int num;
int in_num;
int rev_num;
int digit_1;
int digit_10;
int digit_100;
int digit_1000;
int numb;
bool isPrime;

for (numb=1; numb<=100; numb++)
{

// check if prime
isPrime=true;
if (numb%2 == 0)
{
isPrime=false;
}
else
{
for (int i=3; i<=numb/2; i=i+2)
{
if (numb%i == 0)
{
isPrime=false;
break;
}
}
}

// print if number is prime
if (isPrime)
{
//reverse (isPrime);
cout << numb << "\t";
}
}


return 0;
}
Mar 8, 2008 at 12:12am
Sorry to respond so late...

You are off to a good start. I suggest that you move the stuff that determines whether or not the number is prime into a function called "isPrime":
bool isPrime( int number );

Then you can use it like
1
2
3
4
5
6
7
8
  for (numb=1; numb<=100; numb++)
  {
    if (isPrime( numb ))
    {
      // I've found a possible emirp (because
      // it is prime --it has passed the first test)
    }
  }


Now, I've noticed you've got the idea to reverse a number. Good idea. Your reverse() function should work very simply. Remember, to get the ones place, use the remainder function:
ones_place_value = number % 10;
To shift the number down, simply divide by ten:
number /= 10;
To shift a number UP, multiply by ten:
result *= 10;
You'll need to use a loop to shift the one's digit out of 'number' and into 'result'. Think a little about it and when to stop. You'll get it fine.

Back to main(). Once you know the number is prime, all you have to do is reverse it. If the reverse is equal to the prime, it is not an emirp. If it is different, and if the reverse is also prime, then you can print the number.

Hope this helps.
Mar 8, 2008 at 6:25pm
Hi, I'm back again.
I'm desperate to make this code work.
This is as far as I could get, but is still not working. ??
#include <iostream>
#include <fstream>

using namespace std;
void reverse ();
bool isPrime;

int main()
{
// variables
int rev_numb;
int digit_1;
int digit_10;
int digit_100;
int digit_1000;
int numb;
// bool isPrime ();
for (int numb=1; numb<=10; numb++)
{
// check if prime
isPrime=true;
if (numb%2 == 0)
{
isPrime=false;
}
else
{
for (int i=3; i<=numb/2; i=i+2)
{
if (numb%i == 0)
{
isPrime=false;
break;
}
}
}

// print if number is prime
if (isPrime)
{
reverse ();
cout << numb << "\t";
}
}
// save the number
//bool isPrime=numb;

// strip out the digits
digit_1=numb % 10;
numb=numb / 10;
digit_10=numb % 10;
numb=numb / 10;
digit_100=numb %10;
numb=numb / 10;
digit_1000=numb;

// reverse the number
rev_numb=(digit_1*1000)+(digit_10*100)+(digit_100*10)
+(digit_1000);

// print out the reversed number
cout << "the number reversed = " << rev_numb << "\n\n";

// test to see if it is a palidrome
if (numb == rev_numb)
{
cout << "It is a palidrome.";
}

return 0;
}
Topic archived. No new replies allowed.