d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Need Easy Programming Help > Quick Fix Please
Prev12
Add Reply New Topic New Poll
Member
Posts: 13,618
Joined: Nov 24 2008
Gold: 14,610.00
Dec 5 2014 08:17pm
Quote (Dontrunaway @ Dec 4 2014 06:15pm)
Right. A number is not prime if any number in the loop mod = 0.

Also to be more efficient, you don't have to go to the number divided by 2 as your endpoint. You only have to test numbers up to the square root of the number that you are trying to determine is prime.

i.e. if you are trying to figure out if 10,000 is prime, you only need to mod with numbers up to 100 (or 99).



K soo, 1 quick question, after the variable j cycle, what do i put into it to check all of the numbers and decide to print the primes because i feel like i am missing a full step in that area, like i am testing for 1 number then moving on when i should be doing multiple tests per number
Member
Posts: 21,893
Joined: Mar 27 2009
Gold: 12,408.00
Dec 5 2014 08:24pm
Quote (Elements_Fury @ Dec 5 2014 08:17pm)
K soo, 1 quick question, after the variable j cycle, what do i put into it to check all of the numbers and decide to print the primes because i feel like i am missing a full step in that area, like i am testing for 1 number then moving on when i should be doing multiple tests per number


So if I were to write out the quick logic for this program:

Input number x
y = floor(sqrt(x))
loop i from 1 to x
loop k from 2 to y
if k >= i - 1 then too high, exit k loop (a number mod itself is 0 so we can't run this comparison)
if i mod k = 0 then use boolean to false, exit k loop
else still prime, nothing changes
next k
if boolean still true, print i, else nothing (set boolean back to true)
next i

Then you may have to force a few prime numbers @ the lower ends that you know are prime (1, 2, 3)

e/ Made a few mistakes, edited version should be fine afaik.

So you basically have your prime test numbers (i) 1->x and your test integers (k) 2->y, you compare your prime test number with all test integers up to (but not including) k to see if it can come up with an even division

This post was edited by Dontrunaway on Dec 5 2014 08:45pm
Member
Posts: 13,618
Joined: Nov 24 2008
Gold: 14,610.00
Dec 5 2014 08:45pm
Quote (Dontrunaway @ Dec 6 2014 02:24am)
So if I were to write out the quick logic for this program:

Input number x
y = floor(sqrt(x))
loop i from 1 to x
loop k from 2 to y
if k >= i -1 then too high, exit k loop (a number mod itself is 0 so we can't run this comparison)
if i mod k = 0 then use boolean to false, exit k loop
else still prime, nothing changes
next k
if boolean still true, print i, else nothing (set boolean back to true)
next i

Then you may have to force a few prime numbers @ the lower ends that you know are prime (1, 2, 3)

e/ Made a few mistakes, edited version should be fine afaik.

So you basically have your prime test numbers (i) 1->x and your test integers (k) 2->y, you compare your prime test number with all test integers up to (but not including) k to see if it can come up with an even division


That makes sense now thx
Go Back To Homework Help Topic List
Prev12
Add Reply New Topic New Poll