prime generation

Post here if you don't find any other place for your post. But please, stay on-topic: algorithms, programming or something related to this web site and its services.

Moderator: Board moderators

Post Reply
udvat
New poster
Posts: 29
Joined: Thu Aug 07, 2003 9:56 pm

prime generation

Post by udvat »

I have to find out the primes within the range 100000000. but i dont have the option of having a large array(so i cant use seive). wud anybody plz give me the solution?the code should be faster.
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

You can still declare an array big enough to sieve that. Just pack the booleans in bits instead of bytes.

100,000,000 / 8 bits per byte = 12.5 million bytes (which is less than the 32MB limit)
rjhadley
Learning poster
Posts: 73
Joined: Mon Oct 14, 2002 7:15 am
Location: United States

Post by rjhadley »

You could also try a primality test such as the Rabin-Miller Algorithm. (It's basically a probablistic test along the lines of Fermat's Little Theorem, although there are also deterministic versions).
Caesum
Experienced poster
Posts: 225
Joined: Fri May 03, 2002 12:14 am
Location: UK
Contact:

Post by Caesum »

personally i would code a couple of strong psp tests for the first few primes, and there are well known exceptions to them which you can incorporate in your algorithm if necessary.
udvat
New poster
Posts: 29
Joined: Thu Aug 07, 2003 9:56 pm

help me

Post by udvat »

thankx to u all. am sending my code here. the judge says my code takes a long time to compile and it may because of using more than 16 mb of memory.its a problem from usaco training gateway.name is"prime palindrom"

************code has been removed
Last edited by udvat on Tue Nov 18, 2003 5:05 pm, edited 1 time in total.
LPH
New poster
Posts: 34
Joined: Mon Nov 17, 2003 10:41 am

Re: help me

Post by LPH »

udvat wrote:thankx to u all. am sending my code here. the judge says my code takes a long time to compile and it may because of using more than 16 mb of memory.its a problem from usaco training gateway.name is"prime palindrom"
u can try this:
only generate the prime under 10000 (since the square root of 100000000 (i hope i didn't mislook; there are 8 zeros) is 10000), then when u try to determine whether a number is a prime or not, try to divide it with all the primes from 2 to the square root of the number (that's why we only generate the prime under 10000, 'cause we have enough prime to determine number up to 100000000). if none of them can exactly divide your number, it's a prime; otherwise, it's not a prime.

according to my experience, there are about 12000 primes under 10000(if my memory is right :roll: ).

hope this'll be helpful :)
junjieliang
Experienced poster
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore

Post by junjieliang »

Some hints for you:

1. Instead of checking whether a number is prime and then whether it's a palindrome, consider doing it the other way.

2. There are very few prime palindromes around. According to my code there's less than 10000 of them only *hint hint*.

Good luck!
Caesum
Experienced poster
Posts: 225
Joined: Fri May 03, 2002 12:14 am
Location: UK
Contact:

Post by Caesum »

and obviously there is only one prime palindrome with an even number of digits.....
udvat
New poster
Posts: 29
Joined: Thu Aug 07, 2003 9:56 pm

got AC

Post by udvat »

special thankx to Caesum ,LPH & UFP2161 .following ur suggesstions atlast i got AC.i cant get the hints provided by junjieliang. wud u plz make it easier for me?thanks to all again :wink:
junjieliang
Experienced poster
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore

Post by junjieliang »

Now that I read through my post again I guess I wasn't too clear before. Actually I meant that you generate the palindromes and then check whether they are primes. This will be faster than check every number. Then you can store these palindrome primes into an array and check against the range in the input.
udvat
New poster
Posts: 29
Joined: Thu Aug 07, 2003 9:56 pm

thankx junjieliang

Post by udvat »

yes, its a good idea. i will try it. thank u very much :)
Post Reply

Return to “Other words”