Liberty BASIC Community Forum
« [RC] Factors of an integer »

Welcome Guest. Please Login or Register.
Jan 22nd, 2018, 6:00pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: [RC] Factors of an integer  (Read 257 times)
JackKelly
Guest
xx [RC] Factors of an integer
« Thread started on: Sep 26th, 2016, 03:35am »

http://rosettacode.org/wiki/Factors_of_an_integer

How about this? Any thoughts?

Code:
print "ROSETTA CODE - Factors of an integer"
'A simpler approach for smaller numbers
[Start]
print
input "Enter an integer (< 1,000,000): "; n
n=abs(int(n)): if n=0 then goto [Quit]
if n>999999 then goto [Start]
FactorCount=FactorCount(n)
select case FactorCount
    case 1: print "The factor of 1 is: 1"
    case else
        print "The "; FactorCount; " factors of "; n; " are: ";
        for x=FactorCount to 1 step -1
            print " "; Factor(x);
        next x
        if FactorCount=2 then print " (Prime)" else print
end select
goto [Start]

[Quit]
print "Program complete."
end

function FactorCount(n)
    dim Factor(100)
    for y=1 to n
        if y>sqr(n) and FactorCount=1 then
'If no second factor is found by the square root of n, then n is prime.
            FactorCount=2: Factor(FactorCount)=1: exit function
        end if
        z=n/y
        if z=int(z) then
            FactorCount=FactorCount+1
            Factor(FactorCount)=z
        end if
    next y
end function
 
User IP Logged

JackKelly
Guest
xx Re: [RC] Factors of an integer
« Reply #1 on: Sep 28th, 2016, 01:38am »

Yes, I completely agree -- (n mod y)=0 is better than (n/y)=int(n/y) for determining a factor. But one seems to run just as fast as the other. Isn't it interesting, though, about the square root of n?
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls