Page 1 of 1

newbie programming class question

Posted: Thu Jun 24, 2004 3:32 am
by buckyno2
My instructor wants us to write a program that will ask for a range of numbers and will generate all prime numbers within that range using WHILE loops. I understand about counters but don't know how to get out of two embedded loops (see beginning code below). Can anyone help?

cls
      dim Startnum, Lastnum, bottom as integer

&nbsp; &nbsp; &nbsp; WHILE startnum <=lastnum
&nbsp; &nbsp; &nbsp; bottom=2
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHILE bottom<startnum
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;check=startnum/bottom
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bottom=bottom+1
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WEND

Thats as far as I can get. Help?

buckyno2

Re: newbie programming class question

Posted: Thu Jun 24, 2004 8:19 am
by Guest
For every while you will need a wend.
I only see 1 wend so the program won't run.

second since startnum is never defined to begin with,
it is always going to be less than bottom so the second loop will never get accessed.

third, if you use
DO
whatever
LOOP WHILE condition
then you can use exit do to get out of the loop early if
you need to do that.

lastly, I would divide by all positive numbers less than
the number you are checking and see if it is an integer
one way to do that is:

if nbr/divisor = INT(nbr/divisor) then ...

(since a prime number should only be divisible by itself.)

Re: newbie programming class question

Posted: Thu Jun 24, 2004 8:23 am
by Guest
I should add divisible by only itself and 1. of course.

Re: newbie programming class question

Posted: Thu Jun 24, 2004 9:23 pm
by buckyno2
Thanks buffoasys. I should have included the the input &nbsp;command defining startnum as the first number of the range, lastnum as the second, and bottom as the divisor. My confusion is how to display the prime numbers it does find. The second (interior) loop does the division to determine whether it is prime or not, the first loop would continue with adding a digit (counter) to startnum until lastnum is surpassed. But then what? There will be multiple primes to display. I can't (?) create and display a variable for every prime it finds (p1, p2, p3, etc) and then PRINT each one. How can I tell it to display the primes it finds?

Re: newbie programming class question

Posted: Fri Jun 25, 2004 3:47 am
by Guest
You can divide anything by anything else as long as the disor is not zero so just dividing it doesn't accomplish much.

you could use mod .. but if that's too advanced, use the suggestion about comparing the division with integer of the division. if they are the same then it divides evenly.

if it does divide evenly then it's not a prime number.

bottom=2
isprime%=1
while bottom<startnum
&nbsp;if startnum/bottom=int(startnum/bottom) then &nbsp;isprime%=0
wend
if isprime% then print startnum
...
or you could store the prime number in an array then print out the array at the end of the program

bad%=0

Re: newbie programming class question

Posted: Fri Jun 25, 2004 3:49 am
by Guest
ignore the bad%=0 -- i was using bad% instead of isprime% at first but changed it and just failed to get
rid of that statement.