Qbasicnews.com

Full Version: Prime factors in 25 lines or less
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
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
I can't seem to get Agamemnus's code to work. It just parrots back the number that I enter.[/b]
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:
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
Hello Antoni,
realy nice work.

Joshy
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
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!
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.
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:
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.
Pages: 1 2 3