Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,562 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,884 people online right now. Registration is fast and FREE... Join Now!




Prime Numbers dead ahead

 
Reply to this topicStart new topic

Prime Numbers dead ahead, Is there an easy way to calculate them?

prototype_z12903
10 Apr, 2008 - 06:20 PM
Post #1

New D.I.C Head
*

Joined: 24 Mar, 2008
Posts: 6


My Contributions
is there an easy way to tell if a number is prime other than dividing it by every number up to the one you're testing?

also ive heard about generating random numbers by multiplying huge prime numbers, how exactly, would that work?
are you gererating random prime numbers and multiplying them or is there something else im missing?
and also are c's data types large enough for that kind of stuff?
is it any better than rand() and other similar pseudo - random number gererators?

i'm been looking around at so much stuff the last few days and need to set things straight, i'd apprieciate any help you can give

This post has been edited by prototype_z12903: 10 Apr, 2008 - 06:23 PM
User is offlineProfile CardPM
+Quote Post

gilbert
RE: Prime Numbers Dead Ahead
10 Apr, 2008 - 06:42 PM
Post #2

D.I.C Head
**

Joined: 18 Mar, 2008
Posts: 81


My Contributions
divide it till you get to the halfway point tongue.gif after that the only other numbers except 1 are all decimals

This post has been edited by gilbert: 10 Apr, 2008 - 06:42 PM
User is offlineProfile CardPM
+Quote Post

prototype_z12903
RE: Prime Numbers Dead Ahead
11 Apr, 2008 - 12:47 PM
Post #3

New D.I.C Head
*

Joined: 24 Mar, 2008
Posts: 6


My Contributions
thanks, its an obvious solution i hadn't thought of!

This post has been edited by prototype_z12903: 12 Apr, 2008 - 06:52 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Prime Numbers Dead Ahead
11 Apr, 2008 - 04:08 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
Well there are several ways to calculate prime numbers. Although it is not really the fastest, of the standard algorithms is the Sieve of Eratosthenes.

To implement it is actually really easy. You create an array filled with numbers from 2 to N and then:

a. get the first number in the array and add it to your list of primes.
b. Iterate though the array by multiples of that last prime found setting those numbers to 0.
c. As long as your top prime is less then sqrt(N) then goto step a.
d. If your largest prime is greater than sqrt(N) then add all remaining non-zero numbers in the array to your list of primes.

Once you have an good long list of primes you can use that to check the primarily of all numbers up to square of the largest prime in your list. To do this you just make sure than a number 'A' is not divisible by all primes less than sqrt(A).
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:45PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month