Prime factors in 25 lines or less whodat Junior Member Posts: 31 Threads: 4 Joined: Oct 2005 10-09-2005, 05:57 AM Quote:Code:```INPUT "enter a number: ", x& PRINT "the prime factors are:"; k& = 2 WHILE k& <= x&   WHILE x& MOD k& = 0     x& = x& / k&     PRINT k&;   WEND   k& = k& + 1 WEND PRINT``` - neuro Only 11 lines. My code has 24! :oops: DEFLNG A-Z CLS INPUT "Enter positive integer"; fact PRINT "Prime Factors are: "; srfact = INT(SQR(fact)) DO prime = prime + 2 srprime = INT(SQR(prime)) FOR a = 2 TO srprime IF prime MOD a = 0 THEN EXIT FOR NEXT a IF a = srprime + 1 THEN DO k = fact MOD prime IF k = 0 THEN PRINT prime; fact = fact / prime END IF LOOP UNTIL k > 0 END IF IF prime = 2 THEN prime = 1 IF fact = 1 THEN EXIT DO LOOP UNTIL prime >= srfact IF fact <> 1 THEN PRINT fact whodat Junior Member Posts: 31 Threads: 4 Joined: Oct 2005 10-09-2005, 06:06 AM I can't seem to get Agamemnus's code to work. It just parrots back the number that I enter.[/b] rpgfan3233 Senior Member Posts: 500 Threads: 7 Joined: Jun 2005 10-09-2005, 10:27 AM I would post mine, but it's too similar to neuro's (I think it's the fastest way too). Basically, the only differences are that I used different variable names and DO...WHILE loops rather than WHILE...WEND. Edit: Quote:I can't seem to get Agamemnus's code to work. It just parrots back the number that I enter.[/b] Are you entering a composite number or a prime number when you get "The Parrot Effect"? :lol: 974277320612072617420666C61696C21 (Hexadecimal for those who don't know) Antoni Gual Posting Freak Posts: 1,407 Threads: 117 Joined: Dec 2002 10-09-2005, 10:24 PM Neuro's entry takes ages to factorize 2123456783, it issues a 7 at the start then it stands for a while. I tried to modify it to make it faster, by not checking 2 and 3 multiples and stopping when the factor is equal to the square root of the remainder. Here is the result, 25 lines at all. Code:```declare sub checkf(k&) dim shared x& do   print     INPUT "enter a number to factorize[0 to end] : ", x&     if x&=0 then exit do     PRINT "the prime factors are:";     checkf(2)     checkf(3)     a&=2     k& = 3 + a&     WHILE k& <=int(sqr(x&))       checkf(k&)       k& = k& + a&       a&=6&-a&     WEND     if x&>1 then print x&; loop PRINT "Ended" sub checkf(k&) WHILE x& MOD k& = 0   x& = x& \ k&   PRINT k&; WEND end sub``` Antoni d.j.peters Member Posts: 140 Threads: 13 Joined: Apr 2005 10-09-2005, 11:17 PM Hello Antoni, realy nice work. Joshy sorry about my english Agamemnus Posting Freak Posts: 3,368 Threads: 195 Joined: Jan 2003 10-10-2005, 01:43 AM Nice, methinks you could probably create some sort of string with the values of the first X primes to speed it up too.. or something. Latest: Code:```CLS INPUT "What is the number you want to factor"; testfactor& DO redoloop1: FOR testdiv& = 2 TO testfactor& ^ .5 result1& = testfactor& \ testdiv& IF result1& = testfactor& / testdiv& THEN print testdiv&; testfactor& = result1& goto redoloop1 END IF NEXT testdiv& exit do loop print testfactor&; SLEEP``` Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war." Visit www.neobasic.net to see rubbish in all its finest. whodat Junior Member Posts: 31 Threads: 4 Joined: Oct 2005 10-10-2005, 02:01 AM I can't seem to get Agamemnus's code to work. It just parrots back the number that I enter. Are you entering a composite number or a prime number when you get "The Parrot Effect"? :lol: Composite or prime, it takes your number, exits the loop, prints it, and goes to sleep! Agamemnus Posting Freak Posts: 3,368 Threads: 195 Joined: Jan 2003 10-10-2005, 02:13 AM Maybe you didn't copy it right, because it works fine on my computer.. PS: You only use the code or quote tags once at the beginning and end. Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war." Visit www.neobasic.net to see rubbish in all its finest. Rattrapmax6 Posting Freak Posts: 2,404 Threads: 153 Joined: Jan 2005 10-10-2005, 02:40 AM Quote:Maybe you didn't copy it right, because it works fine on my computer.. PS: You only use the code or quote tags once at the beginning and end. He is using them right, or was before, he must have BBCode disabled or something.. @ Whodat: Go to your profile and check the "Always allow BBCode:", make sure that is selected to "Yes".... Then the BB code will work and you wont have to back it out... :wink: Kevin (x.t.r.GRAPHICS) whodat Junior Member Posts: 31 Threads: 4 Joined: Oct 2005 10-10-2005, 02:58 AM Quote:Maybe you didn't copy it right, because it works fine on my computer.. PS: You only use the code or quote tags once at the beginning and end. Well, your second post did work fine. Thanks for the advice. « Next Oldest | Next Newest »