Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Palindromic Primes

Status
Not open for further replies.

Seijuro

Programmer
Oct 8, 2003
10
US
I would like to print out the Palindromic primes of 5 digit numbers and then get a check sum of the also. Here is my code for printing the primes, but how do I check to see if they are panlindromic??

Code:
#include <stdio.h>

int main(void)
{
    int num=100000;
    int i;

    for(i=2; i<num; i++)
      if(num%2 != 0)
      if(num%3 != 0)
      if(num%4 != 0)
      if(num%5 != 0)
      if(num%6 != 0)
      if(num%7 != 0)
      if(num%8 != 0)
      if(num%9 != 0)
      printf(&quot;%d\n&quot;, num)
}
 
transform number to string and compare symbol at with symbol at[len - i - 1] where i is supposed to be from 0 to len/2 and len is the number of characters in the string.

Ion Filipski
1c.bmp
 
> if(num%9 != 0)
For numbers as large as 100000, you need to check all possible divisors upto and including 316 (which is the approximate square root of 100000)



--
 
Sorry, again tgml processing:
Code:
transform number to string and compare symbol at[i] with symbol at[len - i - 1] where i is supposed to be from 0 to len/2 and len is the number of characters in the string.

Ion Filipski
1c.bmp
 
You seriously, seriously need to check what a prime number is and look at better ways to find them. A prime number is not just a number that doesn't divide by things less than 10. And for that matter, can you find any number that divides by 4 and not by 2?
Have a look at the sieve of Demosthenes (I hope I've got his name right). He was an ancient Greek who had some pretty good ideas on finding primes.
Do NOT, do NOT, please, please please, go looping through every number from 1 to 99999 checking if they all divide by all the others - you can at least skip every second because it divides by 2. There has to be some beauty left in algorithms, otherwise I'm going to give up and become a hairdresser. (no offence to hairdressers)

 
Thanks Salem, I think you corrected me last time I got that wrong, too! One day I'll learn to learn.
Seijuro, if you're reading, it really is a pretty method as well as a big time-saver (and very simple, too) if you're looking for primes of any great size. Well worth a look.
Good luck.


 
the code to find the primes numbers like this...so all I have to do now is find the palindromic primes. Is there a way to find them without using arrays or pointers?

Code:
for (i=1;i<=max;i++)
	{
	 	again: m=(n/k)*k;
	 	if (m!=n)
		k=k+1;
	 	else
		goto try1;
	 	if (k < n/2)
		goto again;
	 	else
		printf(&quot;%d\t&quot;,n);
		printf(&quot;&quot;);
		try1: n=n+1;
		k=2;
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top