Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prime factors in 25 lines or less
#11
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
Reply
#12
I can't seem to get Agamemnus's code to work. It just parrots back the number that I enter.[/b]
Reply
#13
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)
Reply
#14
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
Reply
#15
Hello Antoni,
realy nice work.

Joshy
sorry about my english
Reply
#16
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.
Reply
#17
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!
Reply
#18
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.
Reply
#19
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)

[Image: 11895-r.png]
Reply
#20
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)