|
Post by ivartyn on Jan 7, 2007 23:15:57 GMT
Hi, I'm writting a code that checks for prime numbers as well as as factors of an integer. The user enters the start and end values and the program then checks and displays prime numbers and factors of an integer. I've started but now stuck. Help needed. Thanks
REM Declare Variables DIM startval AS INTEGER DIM endval AS INTEGER DIM totprime AS INTEGER DIM primeflag AS INTEGER DIM n AS INTEGER DIM d AS INTEGER
REM Initialize Variables LET startval = 0 LET endval = 0 LET totprime = 0 LET primeflag = 0 LET n = 0 LET d = 0
REM Clear Screen CLS
REM Prompt User for Satrt and End Values INPUT "Enter start Value:", startval INPUT "Enter End value:", endval
FOR n = startval TO endval IF n > endval THEN PRINT totprime ELSE primeflag = 1 END IF FOR d = startval TO endval IF n / d = INT(n / d) THEN primeflag = 1 END IF NEXT d
NEXT n
|
|
|
Post by earlofqb on Jan 9, 2007 5:24:00 GMT
First, I don't think the first IF...THEN block (IF n>endval) is written properly. A FOR loop will stop at the end of the value, so you must be using it as a form of error checking (making sure there would be no infinite loops and whatnot). However, wouldn't you just end the program now, or use your flag to alert the user and restart the program (so to speak)? If that's the case, I'd test for that condition before even entering the loop. The way you have it written now is confusing, and when you're "stuck" like this, you want to limit confusion. To do that, have an IF...THEN block above your loops (but after the inputs): IF endval>startval THEN 'Alert the user, loop back to inputs ENDIF Also, how are you adding up the total of prime numbers? Primeflag isn't incrementing at all, and totprime isn't used except in the error-trap section I described above. Rather than initialising primeflag to 1 each time you hit a prime, increment totprime. Then when you finish the loops, print totprime. Other than that, I can't see any reason it shouldn't work (of course, it is late here, so forgive me Ildurest if I'm mistaken ).
|
|
|
Post by Ildûrest on Jan 9, 2007 7:28:03 GMT
The program needs a lot of changes, but I'm not sure exactly what it needs to do. I'm a bit confused by "as well as as factors of an integer" - do you want to see if an integer has factors? Or list its factors?
Anyway, 'd' has to start at 2. If the number is divisble by 3, but startval is something like 6, you won't find out.
You're not using the PrimeFlag correctly, either.
|
|
|
Post by earlofqb on Jan 10, 2007 21:02:05 GMT
Righto. Funny how a good night's sleep (or two ) can help you see things (which probably means I should re-do my programming homework, since I did that the same night as troubleshooting his program . Since I re-read it, I too can't figure out what s/he means by that phrase either... Kindly post the requirements and some details as to what you hope to accomplish, Mr/Ms ivartyn.
|
|
buff1
New Member
Posts: 3
|
Post by buff1 on Jan 10, 2007 23:37:21 GMT
How about this LINE INPUT "Enter beginning value ", bv$ LINE INPUT "Enter ending value ", ev$ begval& = VAL(bv$) endval& = VAL(ev$) FOR k& = begval& TO endval& FOR L& = 2 TO endval& - 1 IF k& / L& = INT(k& / L&) AND k& <> L& THEN checkit& = 1 END IF NEXT L& IF checkit& = 0 THEN PRINT k& checkit& = 0 NEXT k&
|
|
|
Post by ivartyn on Jan 19, 2007 0:11:13 GMT
Thanks for the advice given. The program is to take a start value and an end vaue from a user. It should then check all the numbers. At the end the prime numbers should be printed, and the total number of prime numbers shown. Basically a prime number generator.
|
|