I am trying to convert a program that I made in Basic! (QBASIC on iOS) in Python. I am slowly working my way through Python for dummies, but I am fixated on how to convert FOR loops. Can anyone help? Bellow is a QB code.
REM Prime Numbers v2 REM Av 2.2 seconds for 1000 REM Av 5.3 seconds for 2000 INPUT "Prime numbers upto";limit PRINT t1 = TickCount PRINT "2 3 "; count = 2 FOR posprime = 3 TO limit STEP 2 posfactor = 3 prime = 1 GOSUB testing IF prime = 1 THEN PRINT posprime " "; count = count + 1 END IF NEXT posprime t2 = TickCount PRINT PRINT PRINT count " prime numbers found" PRINT USING "####.#"; "Completed in" t2 - t1 " seconds" END testing: IF posprime/posfactor = INT(posprime/posfactor) THEN prime = 0 RETURN ELSE posfactor = posfactor + 2 IF posfactor > SQR(posprime) THEN RETURN ELSE GOTO testing
This is the program that my son and I did on Basic! (QBasic for iOS), which displays all primes to the limit entered by the user, with some added parts that skip the obvious non-primes. Oh, and that includes a little speed check that we're used to seeing the difference between the iPhone and iPad processors.
source share