d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Need Easy Programming Help > Quick Fix Please
12Next
Add Reply New Topic New Poll
Member
Posts: 13,618
Joined: Nov 24 2008
Gold: 14,610.00
Dec 2 2014 07:21pm
I have to make a program that you enter a max number and it calculates all numbers from 0 to that number that is a prime..
I have this so far and it works perfect, but it doesn't print 2 or 3 as primes and im not sure why it won't or how to fix it.

Member
Posts: 39,205
Joined: Jun 25 2006
Gold: 13.21
Dec 2 2014 09:53pm
which language?

post your code also
Member
Posts: 13,618
Joined: Nov 24 2008
Gold: 14,610.00
Dec 3 2014 08:51am
Quote (LeB @ Dec 3 2014 03:53am)
which language?

post your code also


Its just pseudocode in visual logic, it takes the very basics of programming to help you get the fundamentals.
I need it to print all primes up to your max input but It is printing all odds instead of primes, and not 2 or 3 which are primes.

I have it with a for loop 2 to max because 1 isn't prime so it starts at 2, then i did another for loop to check every number for each number (I) to see if it has a divisible number. I went wrong somewhere in my divisible (prime) check.

E: my instructor said its the right idea i however i should do something about saying it is prime at first and checking that after and if it is confirmed output the number?

This post was edited by Elements_Fury on Dec 3 2014 08:54am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 3 2014 01:36pm
looks like this is a problem:

Code
i mod j <> 1


what do you think that's doing?
Member
Posts: 13,618
Joined: Nov 24 2008
Gold: 14,610.00
Dec 3 2014 10:00pm
Quote (carteblanche @ Dec 3 2014 07:36pm)
looks like this is a problem:

Code
i mod j <> 1


what do you think that's doing?


Im not sure, looking for numbers that don't have any dividers, guess i messed it up
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 3 2014 10:24pm
Quote (Elements_Fury @ Dec 3 2014 11:00pm)
Im not sure, looking for numbers that don't have any dividers, guess i messed it up


you need to trace your program by hand. pick an input, then write down everything that happens, including every comparison.
Member
Posts: 13,618
Joined: Nov 24 2008
Gold: 14,610.00
Dec 3 2014 10:53pm
Quote (carteblanche @ Dec 4 2014 04:24am)
you need to trace your program by hand. pick an input, then write down everything that happens, including every comparison.


It seems to be all right because for 2 to max (i), It checks if (j) goes into it and then it repeats until j is greater than i which would mean it tested every possible divisor. I just am stuck on the I mod J part, i know it has to be included but I'm not sure how to actually incorporate it so it will only print the primes
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 3 2014 10:58pm
Quote (Elements_Fury @ Dec 3 2014 11:53pm)
It seems to be all right because for 2 to max (i), It checks if (j) goes into it and then it repeats until j is greater than i which would mean it tested every possible divisor. I just am stuck on the I mod J part, i know it has to be included but I'm not sure how to actually incorporate it so it will only print the primes


explain the significance of 1. why did you choose <> 1 instead of <> 2, or maybe <> 132, or maybe <> 5762? what's special about 1?
Member
Posts: 13,618
Joined: Nov 24 2008
Gold: 14,610.00
Dec 4 2014 09:01am
Quote (carteblanche @ Dec 4 2014 04:58am)
explain the significance of 1. why did you choose <> 1 instead of <> 2, or maybe <> 132, or maybe <> 5762? what's special about 1?


True, it should be 0 because if it has a remainder at all its not evenly divisible
Member
Posts: 21,893
Joined: Mar 27 2009
Gold: 12,408.00
Dec 4 2014 12:15pm
Quote (Elements_Fury @ Dec 4 2014 09:01am)
True, it should be 0 because if it has a remainder at all its not evenly divisible



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).

This post was edited by Dontrunaway on Dec 4 2014 12:20pm
Go Back To Homework Help Topic List
12Next
Add Reply New Topic New Poll