Posts: 31
Threads: 4
Joined: Oct 2005
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
Posts: 31
Threads: 4
Joined: Oct 2005
I can't seem to get Agamemnus's code to work. It just parrots back the number that I enter.[/b]
Posts: 500
Threads: 7
Joined: Jun 2005
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)
Posts: 1,407
Threads: 117
Joined: Dec 2002
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
Posts: 140
Threads: 13
Joined: Apr 2005
Hello Antoni,
realy nice work.
Joshy
sorry about my english
Posts: 3,368
Threads: 195
Joined: Jan 2003
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.
Posts: 31
Threads: 4
Joined: Oct 2005
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!
Posts: 3,368
Threads: 195
Joined: Jan 2003
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.
Posts: 2,404
Threads: 153
Joined: Jan 2005
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)
Posts: 31
Threads: 4
Joined: Oct 2005
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.