Factors of an integer: Difference between revisions

add Refal
m (syntax highlighting fixup automation)
(add Refal)
 
(35 intermediate revisions by 17 users not shown)
Line 1,059:
 
=={{header|BASIC}}==
{{works with|QBasic}}
This example stores the factors in a shared array (with the original number as the last element) for later retrieval.
 
Note that this will error out if you pass 32767 (or higher).
<syntaxhighlight lang="qbasic">DECLARE SUB factor (what AS INTEGER)
 
REDIM SHARED factors(0) AS INTEGER
 
DIM i AS INTEGER, L AS INTEGER
 
INPUT "Gimme a number"; i
 
factor i
 
PRINT factors(0);
FOR L = 1 TO UBOUND(factors)
PRINT ","; factors(L);
NEXT
PRINT
 
SUB factor (what AS INTEGER)
DIM tmpint1 AS INTEGER
DIM L0 AS INTEGER, L1 AS INTEGER
 
REDIM tmp(0) AS INTEGER
REDIM factors(0) AS INTEGER
factors(0) = 1
 
FOR L0 = 2 TO what
IF (0 = (what MOD L0)) THEN
'all this REDIMing and copying can be replaced with:
'REDIM PRESERVE factors(UBOUND(factors)+1)
'in languages that support the PRESERVE keyword
REDIM tmp(UBOUND(factors)) AS INTEGER
FOR L1 = 0 TO UBOUND(factors)
tmp(L1) = factors(L1)
NEXT
REDIM factors(UBOUND(factors) + 1)
FOR L1 = 0 TO UBOUND(factors) - 1
factors(L1) = tmp(L1)
NEXT
factors(UBOUND(factors)) = L0
END IF
NEXT
END SUB</syntaxhighlight>
 
{{out}}
<pre>
Gimme a number? 17
1 , 17
Gimme a number? 12345
1 , 3 , 5 , 15 , 823 , 2469 , 4115 , 12345
Gimme a number? 32765
1 , 5 , 6553 , 32765
Gimme a number? 32766
1 , 2 , 3 , 6 , 43 , 86 , 127 , 129 , 254 , 258 , 381 , 762 , 5461 , 10922 ,
16383 , 32766
</pre>
 
==={{header|Applesoft BASIC}}===
The [[Factors_of_an_integer#Sinclair ZX81 BASIC]] code works the same in Applesoft BASIC.
Line 1,123 ⟶ 1,064:
==={{header|ASIC}}===
{{trans|GW-BASIC}}
<syntaxhighlight lang="basic">REM Factors of an integer
REM Factors of an integer
PRINT "Enter an integer";
LOOP:
Line 1,140 ⟶ 1,080:
END</syntaxhighlight>
{{out}}
<pre>Enter an integer?60
1 2 3 4 5 6 10 12 15 20 30 60</pre>
Enter an integer?60
1 2 3 4 5 6 10 12 15 20 30 60
 
</pre>
 
==={{header|BASIC256}}===
Line 1,163 ⟶ 1,100:
call printFactors(67)
call printFactors(96)
end</syntaxhighlight>
end
 
</syntaxhighlight>
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTLIB"
sort% = FN_sortinit(0, 0)
PRINT "The factors of 45 are " FNfactorlist(45)
PRINT "The factors of 12345 are " FNfactorlist(12345)
END
DEF FNfactorlist(N%)
LOCAL C%, I%, L%(), L$
DIM L%(32)
FOR I% = 1 TO SQR(N%)
IF (N% MOD I% = 0) THEN
L%(C%) = I%
C% += 1
IF (N% <> I%^2) THEN
L%(C%) = (N% DIV I%)
C% += 1
ENDIF
ENDIF
NEXT I%
CALL sort%, L%(0)
FOR I% = 0 TO C%-1
L$ += STR$(L%(I%)) + ", "
NEXT
= LEFT$(LEFT$(L$))</syntaxhighlight>
{{out}}
<pre>The factors of 45 are 1, 3, 5, 9, 15, 45
The factors of 12345 are 1, 3, 5, 15, 823, 2469, 4115, 12345</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|BASIC256}}
<syntaxhighlight lang="qbasic">10 cls
20 printfactors(11)
30 printfactors(21)
40 printfactors(32)
50 printfactors(45)
60 printfactors(67)
70 printfactors(96)
80 end
100 sub printfactors(n)
110 if n < 1 then printfactors = 0
120 print n "=> ";
130 for i = 1 to n/2
140 if n mod i = 0 then print i " ";
150 next i
160 print n
170 end sub</syntaxhighlight>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">do
 
input "enter an integer", n
 
loop n = 0
 
let a = abs(n)
 
for i = 1 to int(a / 2)
 
if a = int(a / i) * i then
 
print i
 
endif
 
next i
 
print a</syntaxhighlight>
{{out| Output}}<pre>?60
1 2 3 4 5 6 10 12 15 20 30 60</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Sub printFactors(n As Integer)
If n < 1 Then Return
Print n; " =>";
For i As Integer = 1 To n / 2
If n Mod i = 0 Then Print i; " ";
Next i
Print n
End Sub
 
printFactors(11)
printFactors(21)
printFactors(32)
printFactors(45)
printFactors(67)
printFactors(96)
Print
Print "Press any key to quit"
Sleep</syntaxhighlight>
{{out}}
<pre>
11 => 1 11
Igual que la entrada de FreeBASIC.
21 => 1 3 7 21
32 => 1 2 4 8 16 32
45 => 1 3 5 9 15 45
67 => 1 67
96 => 1 2 3 4 6 8 12 16 24 32 48 96
</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1, @"Factors of an Integer", (0,0,1000,270)
 
clear local mode
local fn IntegerFactors( f as long ) as CFStringRef
long i, s, l(100), c = 0
CFStringRef factorStr = @""
for i = 1 to sqr(f)
if ( f mod i == 0 )
l(c) = i
c++
if ( f != i ^ 2 )
l(c) = ( f / i )
c++
end if
end if
next i
s = 1
while ( s = 1 )
s = 0
for i = 0 to c-1
if l(i) > l(i+1) and l(i+1) != 0
swap l(i), l(i+1)
s = 1
end if
next i
wend
for i = 0 to c - 1
if ( i < c - 1 )
factorStr = fn StringWithFormat( @"%@ %ld, ", factorStr, l(i) )
else
factorStr = fn StringWithFormat( @"%@ %ld", factorStr, l(i) )
end if
next
end fn = factorStr
 
print @"Factors of 25 are:"; fn IntegerFactors( 25 )
print @"Factors of 45 are:"; fn IntegerFactors( 45 )
print @"Factors of 103 are:"; fn IntegerFactors( 103 )
print @"Factors of 760 are:"; fn IntegerFactors( 760 )
print @"Factors of 12345 are:"; fn IntegerFactors( 12345 )
print @"Factors of 32766 are:"; fn IntegerFactors( 32766 )
print @"Factors of 32767 are:"; fn IntegerFactors( 32767 )
print @"Factors of 57097 are:"; fn IntegerFactors( 57097 )
print @"Factors of 12345678 are:"; fn IntegerFactors( 12345678 )
print @"Factors of 32434243 are:"; fn IntegerFactors( 32434243 )
 
HandleEvents</syntaxhighlight>
{{out}}
<pre>Factors of 25 are: 1, 5, 25
Factors of 45 are: 1, 3, 5, 9, 15, 45
Factors of 103 are: 1, 103
Factors of 760 are: 1, 2, 4, 5, 8, 10, 19, 20, 38, 40, 76, 95, 152, 190, 380, 760
Factors of 12345 are: 1, 3, 5, 15, 823, 2469, 4115, 12345
Factors of 32766 are: 1, 2, 3, 6, 43, 86, 127, 129, 254, 258, 381, 762, 5461, 10922, 16383, 32766
Factors of 32767 are: 1, 7, 31, 151, 217, 1057, 4681, 32767
Factors of 57097 are: 1, 57097
Factors of 12345678 are: 1, 2, 3, 6, 9, 18, 47, 94, 141, 282, 423, 846, 14593, 29186, 43779, 87558, 131337, 262674, 685871, 1371742, 2057613, 4115226, 6172839, 12345678
Factors of 32434243 are: 1, 307, 105649, 32434243</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
printFactors(11)
printFactors(21)
printFactors(32)
printFactors(45)
printFactors(67)
printFactors(96)
 
End
 
Sub printFactors(n As Integer)
 
If n < 1 Then Return
Print n; " =>";
For i As Integer = 1 To n / 2
If n Mod i = 0 Then Print i; " ";
Next
Print n
 
End Sub</syntaxhighlight>
 
==={{header|GW-BASIC}}===
<syntaxhighlight lang="gwbasicqbasic">
10 INPUT "Enter an integer: ", N
20 IF N = 0 THEN GOTO 10
Line 1,193 ⟶ 1,316:
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basicqbasic">100 PROGRAM "Factors.bas"
110 INPUT PROMPT "Number: ":N
120 FOR I=1 TO INT(N/2)
Line 1,199 ⟶ 1,322:
140 NEXT
150 PRINT N</syntaxhighlight>
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">num = 10677106534462215678539721403561279
maxnFactors = 1000
dim primeFactors(maxnFactors), nPrimeFactors(maxnFactors)
global nDifferentPrimeNumbersFound, nFactors, iFactor
 
print "Start finding all factors of ";num; ":"
 
nDifferentPrimeNumbersFound=0
dummy = factorize(num,2)
nFactors = showPrimeFactors(num)
dim factors(nFactors)
dummy = generateFactors(1,1)
sort factors(), 0, nFactors-1
for i=1 to nFactors
print i;" ";factors(i-1)
next i
 
print "done"
 
wait
 
function factorize(iNum,offset)
factorFound=0
i = offset
do
if (iNum MOD i)=0 _
then
if primeFactors(nDifferentPrimeNumbersFound) = i _
then
nPrimeFactors(nDifferentPrimeNumbersFound) = nPrimeFactors(nDifferentPrimeNumbersFound) + 1
else
nDifferentPrimeNumbersFound = nDifferentPrimeNumbersFound + 1
primeFactors(nDifferentPrimeNumbersFound) = i
nPrimeFactors(nDifferentPrimeNumbersFound) = 1
end if
if iNum/i<>1 then dummy = factorize(iNum/i,i)
factorFound=1
end if
i=i+1
loop while factorFound=0 and i<=sqr(iNum)
if factorFound=0 _
then
nDifferentPrimeNumbersFound = nDifferentPrimeNumbersFound + 1
primeFactors(nDifferentPrimeNumbersFound) = iNum
nPrimeFactors(nDifferentPrimeNumbersFound) = 1
end if
end function
 
function showPrimeFactors(iNum)
showPrimeFactors=1
print iNum;" = ";
for i=1 to nDifferentPrimeNumbersFound
print primeFactors(i);"^";nPrimeFactors(i);
if i<nDifferentPrimeNumbersFound then print " * "; else print ""
showPrimeFactors = showPrimeFactors*(nPrimeFactors(i)+1)
next i
end function
 
function generateFactors(product,pIndex)
if pIndex>nDifferentPrimeNumbersFound _
then
factors(iFactor) = product
iFactor=iFactor+1
else
for i=0 to nPrimeFactors(pIndex)
dummy = generateFactors(product*primeFactors(pIndex)^i,pIndex+1)
next i
end if
end function</syntaxhighlight>
{{out}}
 
<syntaxhighlight lang="lb">Start finding all factors of 10677106534462215678539721403561279:
10677106534462215678539721403561279 = 29269^1 * 32579^1 * 98731^2 * 104729^3
1 1
2 29269
3 32579
4 98731
5 104729
6 953554751
7 2889757639
8 3065313101
9 3216557249
10 3411966091
11 9747810361
12 10339998899
13 10968163441
14 94145414120981
15 99864835517479
16 285308661456109
17 302641427774831
18 317573913751019
19 321027175754629
20 336866824130521
21 357331796744339
22 1020878431297169
23 1082897744693371
24 1148684789012489
25 9295070881578575111
26 9859755075476219149
27 10458744358910058191
28 29880090805636839461
29 31695334089430275799
30 33259198413230468851
31 33620855089606540541
32 35279725624365333809
33 37423001741237879131
34 106915577231321212201
35 113410797903992051459
36 973463478356842592799919
37 1032602289299548955255621
38 1095333837964291484285239
39 3129312029983540559911069
40 3319420643851943354153471
41 3483202590619213772296379
42 3694810384914157044482761
43 11197161487859039232598529
44 101949856624833767901342716951
45 108143405156052462534965931709
46 327729719588146219298926345301
47 364792324112959639158827476291
48 10677106534462215678539721403561279
done</syntaxhighlight>
 
====A Simpler Approach====
This is a somewhat simpler approach for finding the factors of smaller numbers (less than one million).
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">
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=1 to FactorCount
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)=n: exit function
end if
if (n mod y)=0 then
FactorCount=FactorCount+1
Factor(FactorCount)=y
end if
next y
end function
</syntaxhighlight>
{{out}}
<pre>
ROSETTA CODE - Factors of an integer
 
Enter an integer (< 1,000,000): 1
The factor of 1 is: 1
 
Enter an integer (< 1,000,000): 2
The 2 factors of 2 are: 1 2 (Prime)
 
Enter an integer (< 1,000,000): 4
The 3 factors of 4 are: 1 2 4
 
Enter an integer (< 1,000,000): 6
The 4 factors of 6 are: 1 2 3 6
 
Enter an integer (< 1,000,000): 999999
The 64 factors of 999999 are: 1 3 7 9 11 13 21 27 33 37 39 63 77 91 99 111 117 143 189 231 259 273 297 333 351 407 429 481 693 777 819 999 1001 1221 1287 1443 2079 2331 2457 2849 3003 3367 3663 3861 4329 5291 6993 8547 9009 10101 10989 12987 15873 25641 27027 30303 37037 47619 76923 90909 111111 142857 333333 999999
 
Enter an integer (< 1,000,000):
Program complete.
</pre>
 
==={{header|Minimal BASIC}}===
{{trans|GW-BASIC}}
{{works with|Commodore BASIC}}
{{works with|IS-BASIC}}
{{works with|MSX BASIC|any}}
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="gwbasicqbasic">10 REM Factors of an integer
10 REM Factors of an integer
20 PRINT "Enter an integer";
30 INPUT N
Line 1,215 ⟶ 1,527:
90 NEXT I
100 PRINT N1
110 END</syntaxhighlight>
 
</syntaxhighlight>
==={{header|MSX Basic}}===
{{trans|GW-BASIC}}
<syntaxhighlight lang="qbasic">10 INPUT "Enter an integer: "; N
20 IF N = 0 THEN GOTO 10
30 N1 = ABS(N)
40 FOR I = 1 TO N1/2
50 IF N1 MOD I = 0 THEN PRINT I;
60 NEXT I
70 PRINT N1</syntaxhighlight>
 
==={{header|Nascom BASIC}}===
Line 1,233 ⟶ 1,554:
</syntaxhighlight>
{{out}}
<pre>Enter an integer? 60
<pre>
1 2 3 4 5 6 10 12 15 20 30 60</pre>
Enter an integer? 60
1 2 3 4 5 6 10 12 15 20 30 60
</pre>
See also [[#Minimal BASIC|Minimal BASIC]]
 
==={{header|Palo Alto Tiny BASIC}}===
{{trans|GW-BASIC}}
<syntaxhighlight lang="basic">
10 REM FACTORS OF AN INTEGER
20 INPUT "ENTER AN INTEGER"N
30 IF N=0 GOTO 20
40 LET A=ABS(N)
50 IF A=1 GOTO 90
60 FOR I=1 TO A/2
70 IF (A/I)*I=A PRINT I," ",
80 NEXT I
90 PRINT A
100 STOP
</syntaxhighlight>
{{out}}
3 runs.
<pre>ENTER AN INTEGER:1
1</pre>
<pre>ENTER AN INTEGER:60
1 2 3 4 5 6 10 12 15 20 30 60</pre>
<pre>ENTER AN INTEGER:-22222
1 2 41 82 271 542 11111 22222</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure PrintFactors(n)
Protected i, lim=Round(sqr(n),#PB_Round_Up)
NewList F.i()
For i=1 To lim
If n%i=0
AddElement(F()): F()=i
AddElement(F()): F()=n/i
EndIf
Next
;- Present the result
SortList(F(),#PB_Sort_Ascending)
ForEach F()
Print(str(F())+" ")
Next
EndProcedure
 
If OpenConsole()
Print("Enter integer to factorize: ")
PrintFactors(Val(Input()))
Print(#CRLF$+#CRLF$+"Press ENTER to quit."): Input()
EndIf</syntaxhighlight>
{{out}}
<pre> Enter integer to factorize: 96
1 2 3 4 6 8 12 16 24 32 48 96</pre>
 
==={{header|QB64}}===
<syntaxhighlight lang="qb64">'Task
'Compute the factors of a positive integer.
 
'These factors are the positive integers by which the number being factored can be divided to yield a positive integer result.
Dim Dividendum As Integer, Index As Integer
Randomize Timer
Dividendum = Int(Rnd * 1000) + 1
Print " Dividendum: "; Dividendum
Index = Int(Dividendum / 2)
print "Divisors: ";
While Index > 0
If Dividendum Mod Index = 0 Then Print Index; " ";
Index = Index - 1
Wend
End</syntaxhighlight>
 
==={{header|QBasic}}===
See [[#QuickBASIC|QuickBASIC]].
 
==={{header|QuickBASIC}}===
{{works with|QBasic}}
This example stores the factors in a shared array (with the original number as the last element) for later retrieval.
 
Note that this will error out if you pass 32767 (or higher).
<syntaxhighlight lang="qbasic">DECLARE SUB factor (what AS INTEGER)
 
REDIM SHARED factors(0) AS INTEGER
 
DIM i AS INTEGER, L AS INTEGER
 
INPUT "Gimme a number"; i
 
factor i
 
PRINT factors(0);
FOR L = 1 TO UBOUND(factors)
PRINT ","; factors(L);
NEXT
PRINT
 
SUB factor (what AS INTEGER)
DIM tmpint1 AS INTEGER
DIM L0 AS INTEGER, L1 AS INTEGER
 
REDIM tmp(0) AS INTEGER
REDIM factors(0) AS INTEGER
factors(0) = 1
 
FOR L0 = 2 TO what
IF (0 = (what MOD L0)) THEN
'all this REDIMing and copying can be replaced with:
'REDIM PRESERVE factors(UBOUND(factors)+1)
'in languages that support the PRESERVE keyword
REDIM tmp(UBOUND(factors)) AS INTEGER
FOR L1 = 0 TO UBOUND(factors)
tmp(L1) = factors(L1)
NEXT
REDIM factors(UBOUND(factors) + 1) AS INTEGER
FOR L1 = 0 TO UBOUND(factors) - 1
factors(L1) = tmp(L1)
NEXT
factors(UBOUND(factors)) = L0
END IF
NEXT
END SUB</syntaxhighlight>
{{out}}
<pre> Gimme a number? 17
1 , 17
Gimme a number? 12345
1 , 3 , 5 , 15 , 823 , 2469 , 4115 , 12345
Gimme a number? 32765
1 , 5 , 6553 , 32765
Gimme a number? 32766
1 , 2 , 3 , 6 , 43 , 86 , 127 , 129 , 254 , 258 , 381 , 762 , 5461 , 10922 ,
16383 , 32766</pre>
 
==={{header|Quite BASIC}}===
{{trans|GW-BASIC}}
<syntaxhighlight lang="qbasic">10 INPUT "Enter an integer: "; N
20 IF N = 0 THEN GOTO 15
30 N1 = ABS(N)
40 FOR I = 1 TO N1/2
50 IF N1 - INT(N1 / I) * I = 0 THEN PRINT I; " ";
60 NEXT I
70 PRINT N1</syntaxhighlight>
 
==={{header|REALbasic}}===
<syntaxhighlight lang="vb">Function factors(num As UInt64) As UInt64()
'This function accepts an unsigned 64 bit integer as input and returns an array of unsigned 64 bit integers
Dim result() As UInt64
Dim iFactor As UInt64 = 1
While iFactor <= num/2 'Since a factor will never be larger than half of the number
If num Mod iFactor = 0 Then
result.Append(iFactor)
End If
iFactor = iFactor + 1
Wend
result.Append(num) 'Since a given number is always a factor of itself
Return result
End Function</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="basic">PRINT "Factors of 45 are ";factorlist$(45)
PRINT "Factors of 12345 are "; factorlist$(12345)
END
FUNCTION factorlist$(f)
DIM L(100)
FOR i = 1 TO SQR(f)
IF (f MOD i) = 0 THEN
L(c) = i
c = c + 1
IF (f <> i^2) THEN
L(c) = (f / i)
c = c + 1
END IF
END IF
NEXT i
s = 1
WHILE s = 1
s = 0
FOR i = 0 TO c-1
IF L(i) > L(i+1) AND L(i+1) <> 0 THEN
t = L(i)
L(i) = L(i+1)
L(i+1) = t
s = 1
END IF
NEXT i
WEND
FOR i = 0 TO c-1
factorlist$ = factorlist$ + STR$(L(i)) + ", "
NEXT
END FUNCTION</syntaxhighlight>
{{out}}
<pre>Factors of 45 are 1, 3, 5, 9, 15, 45,
Factors of 12345 are 1, 3, 5, 15, 823, 2469, 4115, 12345, </pre>
 
==={{header|Sinclair ZX81 BASIC}}===
Line 1,251 ⟶ 1,758:
 
==={{header|Tiny BASIC}}===
{{works with|TinyBasic}}
<syntaxhighlight lang="tiny basic">100 PRINT "Give me a number:"
<syntaxhighlight lang="basic">100 PRINT "Give me a number:"
110 INPUT I
120 LET C=1
Line 1,274 ⟶ 1,782:
20
30
60</pre>
</pre>
 
 
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">SUB printfactors(n)
IF n < 1 THEN EXIT SUB
sub printfactors(n)
ifPRINT n; < 1 then exit sub"=>";
printFOR n;i "=>"; 1 TO n / 2
IF REMAINDER(n, i) = 0 THEN PRINT i;
NEXT i
PRINT n
END SUB
 
CALL printfactors(11)
CALL printfactors(21)
CALL printfactors(32)
CALL printfactors(45)
CALL printfactors(67)
CALL printfactors(96)
END</syntaxhighlight>
 
==={{header|VBA}}===
<syntaxhighlight lang="vb">Function Factors(x As Integer) As String
Application.Volatile
Dim i As Integer
Dim cooresponding_factors As String
Factors = 1
corresponding_factors = x
For i = 2 To Sqr(x)
If x Mod i = 0 Then
Factors = Factors & ", " & i
If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors
End If
Next i
If x <> 1 Then Factors = Factors & ", " & corresponding_factors
End Function</syntaxhighlight>
{{out}}
<pre>cell formula is "=Factors(840)"
resultant value is "1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 15, 20, 21, 24, 28, 30, 35, 40, 42, 56, 60, 70, 84, 105, 120, 140, 168, 210, 280, 420, 840"</pre>
 
==={{header|XBasic}}===
{{trans|BASIC256}}
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Factors of an integer"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION printFactors (n)
 
FUNCTION Entry ()
printFactors(11)
printFactors(21)
printFactors(32)
printFactors(45)
printFactors(67)
printFactors(96)
END FUNCTION
 
FUNCTION printFactors (n)
PRINT n; " =>";
FOR i = 1 TO n / 2
IF n MOD i = 0 THEN PRINT i; " ";
NEXT i
PRINT n
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">sub printFactors(n)
if n < 1 return 0
print n, " =>";
for i = 1 to n / 2
if remaindermod(n, i) = 0 then print i, " ";
next i
print n
end sub
 
printFactors(11)
call printfactors(11)
printFactors(21)
call printfactors(21)
printFactors(32)
call printfactors(32)
printFactors(45)
call printfactors(45)
printFactors(67)
call printfactors(67)
printFactors(96)
call printfactors(96)
print
end</syntaxhighlight>
end
 
</syntaxhighlight>
==={{header|ZX Spectrum Basic}}===
{{out}}
{{trans|AWK}}
<pre>
<syntaxhighlight lang="basic">10 INPUT "Enter a number or 0 to exit: ";n
Igual que la entrada de FreeBASIC.
20 IF n=0 THEN STOP
</pre>
30 PRINT "Factors of ";n;": ";
40 FOR i=1 TO n
50 IF FN m(n,i)=0 THEN PRINT i;" ";
60 NEXT i
70 DEF FN m(a,b)=a-INT (a/b)*b</syntaxhighlight>
 
=={{header|Batch File}}==
Line 1,352 ⟶ 1,927:
Gimme a number:102
Factors of 102: 1 2 3 6 17 34 51 102</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTLIB"
sort% = FN_sortinit(0, 0)
PRINT "The factors of 45 are " FNfactorlist(45)
PRINT "The factors of 12345 are " FNfactorlist(12345)
END
DEF FNfactorlist(N%)
LOCAL C%, I%, L%(), L$
DIM L%(32)
FOR I% = 1 TO SQR(N%)
IF (N% MOD I% = 0) THEN
L%(C%) = I%
C% += 1
IF (N% <> I%^2) THEN
L%(C%) = (N% DIV I%)
C% += 1
ENDIF
ENDIF
NEXT I%
CALL sort%, L%(0)
FOR I% = 0 TO C%-1
L$ += STR$(L%(I%)) + ", "
NEXT
= LEFT$(LEFT$(L$))</syntaxhighlight>
 
{{out}}
<pre>The factors of 45 are 1, 3, 5, 9, 15, 45
The factors of 12345 are 1, 3, 5, 15, 823, 2469, 4115, 12345</pre>
 
=={{header|bc}}==
Line 1,644 ⟶ 2,187:
} while (true);
}</syntaxhighlight>
{{out}}
<pre>Number:
32434243
For 1 through 5695
Found: 1. 32434243 / 1 = 32434243
Found: 307. 32434243 / 307 = 105649
Done.</pre>
 
===C# 3.0===
Line 1,663 ⟶ 2,213:
 
{{out}}
<pre>1, 3, 5, 9, 15, 45</pre>
<pre>Number:
32434243
For 1 through 5695
Found: 1. 32434243 / 1 = 32434243
Found: 307. 32434243 / 307 = 105649
Done.</pre>
 
=={{header|C++}}==
Line 2,363 ⟶ 2,908:
factor prime count : 1229, 7.316 sec
divisor prime count : 1229, 0.265 sec
</pre>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun factors = List by int n
List result = int[1]
for each int i in range(2, n)
if n % i == 0 do result.append(i) end
end
result.append(n)
return result
end
fun main = int by List args
int n = when(args.length == 0, ask(int, "Enter the number to factor please "), int!args[0])
writeLine(factors(n))
return 0
end
exit main(Runtime.args)
</syntaxhighlight>
{{out}}
<pre>
emal.exe Org\RosettaCode\FactorsOfAnInteger.emal 999997
[1,757,1321,999997]
</pre>
 
Line 2,857 ⟶ 3,425:
end program</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Sub printFactors(n As Integer)
If n < 1 Then Return
Print n; " =>";
For i As Integer = 1 To n / 2
If n Mod i = 0 Then Print i; " ";
Next i
Print n
End Sub
 
printFactors(11)
printFactors(21)
printFactors(32)
printFactors(45)
printFactors(67)
printFactors(96)
Print
Print "Press any key to quit"
Sleep</syntaxhighlight>
 
{{out}}
<pre>
11 => 1 11
21 => 1 3 7 21
32 => 1 2 4 8 16 32
45 => 1 3 5 9 15 45
67 => 1 67
96 => 1 2 3 4 6 8 12 16 24 32 48 96
</pre>
 
=={{header|Frink}}==
Line 2,917 ⟶ 3,453:
The set of factors of 639 is {9, 639, 71, 213, 1, 3}
The set of factors of 760 is {8, 19, 4, 40, 152, 5, 10, 76, 1, 95, 190, 760, 20, 2, 38, 380}
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">window 1, @"Factors of an Integer", (0,0,1000,270)
 
clear local mode
local fn IntegerFactors( f as long ) as CFStringRef
long i, s, l(100), c = 0
CFStringRef factorStr = @""
for i = 1 to sqr(f)
if ( f mod i == 0 )
l(c) = i
c++
if ( f != i ^ 2 )
l(c) = ( f / i )
c++
end if
end if
next i
s = 1
while ( s = 1 )
s = 0
for i = 0 to c-1
if l(i) > l(i+1) and l(i+1) != 0
swap l(i), l(i+1)
s = 1
end if
next i
wend
for i = 0 to c - 1
if ( i < c - 1 )
factorStr = fn StringWithFormat( @"%@ %ld, ", factorStr, l(i) )
else
factorStr = fn StringWithFormat( @"%@ %ld", factorStr, l(i) )
end if
next
end fn = factorStr
 
print @"Factors of 25 are:"; fn IntegerFactors( 25 )
print @"Factors of 45 are:"; fn IntegerFactors( 45 )
print @"Factors of 103 are:"; fn IntegerFactors( 103 )
print @"Factors of 760 are:"; fn IntegerFactors( 760 )
print @"Factors of 12345 are:"; fn IntegerFactors( 12345 )
print @"Factors of 32766 are:"; fn IntegerFactors( 32766 )
print @"Factors of 32767 are:"; fn IntegerFactors( 32767 )
print @"Factors of 57097 are:"; fn IntegerFactors( 57097 )
print @"Factors of 12345678 are:"; fn IntegerFactors( 12345678 )
print @"Factors of 32434243 are:"; fn IntegerFactors( 32434243 )
 
HandleEvents</syntaxhighlight>
 
Output:
<pre>
Factors of 25 are: 1, 5, 25
Factors of 45 are: 1, 3, 5, 9, 15, 45
Factors of 103 are: 1, 103
Factors of 760 are: 1, 2, 4, 5, 8, 10, 19, 20, 38, 40, 76, 95, 152, 190, 380, 760
Factors of 12345 are: 1, 3, 5, 15, 823, 2469, 4115, 12345
Factors of 32766 are: 1, 2, 3, 6, 43, 86, 127, 129, 254, 258, 381, 762, 5461, 10922, 16383, 32766
Factors of 32767 are: 1, 7, 31, 151, 217, 1057, 4681, 32767
Factors of 57097 are: 1, 57097
Factors of 12345678 are: 1, 2, 3, 6, 9, 18, 47, 94, 141, 282, 423, 846, 14593, 29186, 43779, 87558, 131337, 262674, 685871, 1371742, 2057613, 4115226, 6172839, 12345678
Factors of 32434243 are: 1, 307, 105649, 32434243
</pre>
 
Line 3,176 ⟶ 3,646:
Returns list of factors out of order, e.g.:
 
<Langsyntaxhighlight lang="haskell">~> factors 42
[1,7,3,21,2,14,6,42]</syntaxhighlight>
 
Line 3,280 ⟶ 3,750:
 
{{libheader|Icon Programming Library}} [http://www.cs.arizona.edu/icon/library/src/procs/factors.icn divisors]
 
=={{Header|Insitux}}==
 
{{Trans|Clojure}}
 
<syntaxhighlight lang="insitux">
(function factors n
(filter (div? n) (range 1 (inc n))))
 
(factors 45)
</syntaxhighlight>
 
{{out}}
 
<pre>
[1 3 5 9 15 45]
</pre>
 
=={{header|J}}==
Line 3,320 ⟶ 3,807:
However, a data structure which is organized around the prime decomposition of the argument can be hard to read. So, for reader convenience, we should probably arrange them in a monotonically increasing list:
 
<syntaxhighlight lang="j"> factorsfactrst=: [: /:~@, */&>@{@((^ i.@>:)&.>/)@q:~&__
factorsfactrst 420
1 2 3 4 5 6 7 10 12 14 15 20 21 28 30 35 42 60 70 84 105 140 210 420</syntaxhighlight>
 
Line 3,355 ⟶ 3,842:
factors=: (*/@:^"1 odometer@:>:)/@q:~&__</syntaxhighlight>
 
See also the J essays [[j:Essays/Odometer|Odometer]] and [[j:Essays/Divisors|Divisors]].
See http://www.jsoftware.com/jwiki/Essays/Odometer
 
=={{header|Java}}==
Line 3,625 ⟶ 4,112:
<syntaxhighlight lang="julia">using Primes
 
""" Return the factors of n, including 1, n """
function factors(n)
function factors(n::T)::Vector{T} where T <: Integer
f = [one(n)]
sort(vec(map(prod, Iterators.product((p.^(0:m) for (p,e m) in factoreachfactor(n))...))))
f = reduce(vcat, [f*p^j for j in 1:e], init=f)
end
return length(f) == 1 ? [one(n), n] : sort!(f)
end
 
Line 3,647 ⟶ 4,131:
The factors of 53 are: [1, 53]
0.000102 seconds (35 allocations: 1.516 KiB)
 
 
The factors of 64 are: [1, 2, 4, 8, 16, 32, 64]
0.000093 seconds (56 allocations: 3.172 KiB)
Line 3,774 ⟶ 4,260:
> (factors 10677106534462215678539721403561279)
(104729 104729 104729 98731 98731 32579 29269 1)
</pre>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">num = 10677106534462215678539721403561279
maxnFactors = 1000
dim primeFactors(maxnFactors), nPrimeFactors(maxnFactors)
global nDifferentPrimeNumbersFound, nFactors, iFactor
 
 
print "Start finding all factors of ";num; ":"
 
nDifferentPrimeNumbersFound=0
dummy = factorize(num,2)
nFactors = showPrimeFactors(num)
dim factors(nFactors)
dummy = generateFactors(1,1)
sort factors(), 0, nFactors-1
for i=1 to nFactors
print i;" ";factors(i-1)
next i
 
print "done"
 
wait
 
 
function factorize(iNum,offset)
factorFound=0
i = offset
do
if (iNum MOD i)=0 _
then
if primeFactors(nDifferentPrimeNumbersFound) = i _
then
nPrimeFactors(nDifferentPrimeNumbersFound) = nPrimeFactors(nDifferentPrimeNumbersFound) + 1
else
nDifferentPrimeNumbersFound = nDifferentPrimeNumbersFound + 1
primeFactors(nDifferentPrimeNumbersFound) = i
nPrimeFactors(nDifferentPrimeNumbersFound) = 1
end if
if iNum/i<>1 then dummy = factorize(iNum/i,i)
factorFound=1
end if
i=i+1
loop while factorFound=0 and i<=sqr(iNum)
if factorFound=0 _
then
nDifferentPrimeNumbersFound = nDifferentPrimeNumbersFound + 1
primeFactors(nDifferentPrimeNumbersFound) = iNum
nPrimeFactors(nDifferentPrimeNumbersFound) = 1
end if
end function
 
 
function showPrimeFactors(iNum)
showPrimeFactors=1
print iNum;" = ";
for i=1 to nDifferentPrimeNumbersFound
print primeFactors(i);"^";nPrimeFactors(i);
if i<nDifferentPrimeNumbersFound then print " * "; else print ""
showPrimeFactors = showPrimeFactors*(nPrimeFactors(i)+1)
next i
end function
 
 
function generateFactors(product,pIndex)
if pIndex>nDifferentPrimeNumbersFound _
then
factors(iFactor) = product
iFactor=iFactor+1
else
for i=0 to nPrimeFactors(pIndex)
dummy = generateFactors(product*primeFactors(pIndex)^i,pIndex+1)
next i
end if
end function</syntaxhighlight>
 
{{out}}
<syntaxhighlight lang="lb">Start finding all factors of 10677106534462215678539721403561279:
10677106534462215678539721403561279 = 29269^1 * 32579^1 * 98731^2 * 104729^3
1 1
2 29269
3 32579
4 98731
5 104729
6 953554751
7 2889757639
8 3065313101
9 3216557249
10 3411966091
11 9747810361
12 10339998899
13 10968163441
14 94145414120981
15 99864835517479
16 285308661456109
17 302641427774831
18 317573913751019
19 321027175754629
20 336866824130521
21 357331796744339
22 1020878431297169
23 1082897744693371
24 1148684789012489
25 9295070881578575111
26 9859755075476219149
27 10458744358910058191
28 29880090805636839461
29 31695334089430275799
30 33259198413230468851
31 33620855089606540541
32 35279725624365333809
33 37423001741237879131
34 106915577231321212201
35 113410797903992051459
36 973463478356842592799919
37 1032602289299548955255621
38 1095333837964291484285239
39 3129312029983540559911069
40 3319420643851943354153471
41 3483202590619213772296379
42 3694810384914157044482761
43 11197161487859039232598529
44 101949856624833767901342716951
45 108143405156052462534965931709
46 327729719588146219298926345301
47 364792324112959639158827476291
48 10677106534462215678539721403561279
done</syntaxhighlight>
 
===A Simpler Approach===
This is a somewhat simpler approach for finding the factors of smaller numbers (less than one million).
 
<syntaxhighlight lang="lb">
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=1 to FactorCount
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)=n: exit function
end if
if (n mod y)=0 then
FactorCount=FactorCount+1
Factor(FactorCount)=y
end if
next y
end function
</syntaxhighlight>
 
{{out}}
<pre>
ROSETTA CODE - Factors of an integer
 
Enter an integer (< 1,000,000): 1
The factor of 1 is: 1
 
Enter an integer (< 1,000,000): 2
The 2 factors of 2 are: 1 2 (Prime)
 
Enter an integer (< 1,000,000): 4
The 3 factors of 4 are: 1 2 4
 
Enter an integer (< 1,000,000): 6
The 4 factors of 6 are: 1 2 3 6
 
Enter an integer (< 1,000,000): 999999
The 64 factors of 999999 are: 1 3 7 9 11 13 21 27 33 37 39 63 77 91 99 111 117 143 189 231 259 273 297 333 351 407 429 481 693 777 819 999 1001 1221 1287 1443 2079 2331 2457 2849 3003 3367 3663 3861 4329 5291 6993 8547 9009 10101 10989 129
87 15873 25641 27027 30303 37037 47619 76923 90909 111111 142857 333333 999999
 
Enter an integer (< 1,000,000):
Program complete.
</pre>
 
Line 4,449 ⟶ 4,741:
List.filter (fun v -> (n mod v) = 0) (range n)</syntaxhighlight>
 
=={{header|Odin}}==
Uses built-in dynamic arrays, and only checks up to the square root
<syntaxhighlight lang="odin">
package main
 
import "core:fmt"
import "core:slice"
 
factors :: proc(n: int) -> [dynamic]int {
d := 1
factors := make([dynamic]int)
 
for {
q := n / d
r := n % d
 
if d >= q {
if d == q && r == 0 {
append(&factors, d)
}
slice.sort(factors[:])
return factors
}
if r == 0 {
append(&factors, d, q)
}
 
d += 1
}
}
 
main :: proc() {
for n in ([?]int{100, 108, 999, 255, 256, 257}) {
a := factors(n)
fmt.println("The factors of", n, "are", a)
delete(a)
}
}
</syntaxhighlight>
{{Out}}
<pre>
The factors of 100 are [1, 2, 4, 5, 10, 20, 25, 50, 100]
The factors of 108 are [1, 2, 3, 4, 6, 9, 12, 18, 27, 36, 54, 108]
The factors of 999 are [1, 3, 9, 27, 37, 111, 333, 999]
The factors of 255 are [1, 3, 5, 15, 17, 51, 85, 255]
The factors of 256 are [1, 2, 4, 8, 16, 32, 64, 128, 256]
The factors of 257 are [1, 257]
</pre>
=={{header|Oforth}}==
 
Line 5,068 ⟶ 5,408:
{1,3,5,15,823,2469,4115,12345}
</pre>
You can find the implementation of factors(), prime_factors(), and prime_factorsprime_powers() in builtins\pfactors.e,<br>
and mpz_factors(), mpz_prime_factors(), and mpz_pollard_rho() in mpfr.e for larger numbers, for example:
<div style="font-size: 11px">
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- [p2js/integer() bugs]</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3491888400</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"factors"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {2,3,4,5,"...",698377680,872972100,1163962800.0,1745944200.0," (1,918 factors)"}</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3491888400</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"factors"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {2,3,4,5,"...",698377680,872972100,1163962800.0,1745944200.0," (1,918 factors)"}
-- If the include1 parameter is 1 or "BOTH", then you'll also get 1 and 3491888400</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3491888400</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {2,3,5,7,11,13,17,19}</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">prime_powers</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3491888400</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- <nowiki>{{</nowiki>2,4},{3,3},{5,2},{7,1},{11,1},{13,1},{17,1},{19,1<nowiki>}}</nowiki></span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"3491888400"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {2,3,5,7,11,13,17,19}</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">mpz_prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"3491888400"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- <nowiki>{{</nowiki>2,4},{3,3},{5,2},{7,1},{11,1},{13,1},{17,1},{19,1<nowiki>}}</nowiki>
-- Note that mpz_prime_factors() only accepts string or mpz, and not a raw native atom/integer.</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">108233175859200</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- 666</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">108233175859200</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- 666</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"10677106534462215678539721403561279"</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">mpz_prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- <nowiki>{{</nowiki>29269,1},{32579,1},{98731,2},{104729,3<nowiki>}}</nowiki></span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"factors"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {1,29269,"...","364792324112959639158827476291","10677106534462215678539721403561279"," (48 factors)"}</span>
<!--</syntaxhighlight>-->
</div>
Note the value in (string) d exceeds the precision limit of an IEEE-754 float, and would trigger a suitable human readable run-time error if passed to any of the non-mpz routines. Sadly, 1200034005600070000008900000000000000000 exceeds the capabilities of my mpz_pollard_rho(), which I had hoped to showcase - perhaps you would like to improve it?
 
=={{header|Phixmonti}}==
Line 5,180 ⟶ 5,541:
*Finished
END:</syntaxhighlight>
 
=={{header|PL/0}}==
{{trans|GW-BASIC}}
PL/0 does not handle strings. So, no prompt. The program waits for entering a number, and then displays the factors.
<syntaxhighlight lang="pascal">
var n, absn, ndiv2, i;
begin
? n;
absn := n;
if n < 0 then absn := -n;
ndiv2 := absn / 2;
i := 1;
while i <= ndiv2 do
begin
if (absn / i) * i = absn then ! i;
i := i + 1
end;
! absn;
end.
</syntaxhighlight>
4 runs.
{{in}}
<pre>1</pre>
{{out}}
<pre>
1
</pre>
{{in}}
<pre>11</pre>
{{out}}
<pre>
1
2
3
4
6
12
</pre>
{{in}}
<pre>13</pre>
{{out}}
<pre>
1
13
</pre>
{{in}}
<pre>-22222</pre>
{{out}}
<pre>
1
2
41
82
271
542
11111
22222
</pre>
 
=={{header|PL/I}}==
Line 5,410 ⟶ 5,829:
N = 32767, Factors = [1, 7, 31, 151, 217, 1057, 4681, 32767] ;
N = 32767, Factors = [1, 7, 31, 151, 217, 1057, 4681, 32767] .
</pre>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">Procedure PrintFactors(n)
Protected i, lim=Round(sqr(n),#PB_Round_Up)
NewList F.i()
For i=1 To lim
If n%i=0
AddElement(F()): F()=i
AddElement(F()): F()=n/i
EndIf
Next
;- Present the result
SortList(F(),#PB_Sort_Ascending)
ForEach F()
Print(str(F())+" ")
Next
EndProcedure
 
If OpenConsole()
Print("Enter integer to factorize: ")
PrintFactors(Val(Input()))
Print(#CRLF$+#CRLF$+"Press ENTER to quit."): Input()
EndIf</syntaxhighlight>
 
{{out}}
<pre>
Enter integer to factorize: 96
1 2 3 4 6 8 12 16 24 32 48 96
</pre>
 
Line 5,454 ⟶ 5,844:
 
Much better (realize that factors come in pairs, the smaller of which is no bigger than sqrt(n)):
<syntaxhighlight lang="python">>>> from math import sqrtisqrt
>>> def factor(n):
factors1, factorsfactors2 = set()[], []
for x in range(1, int(sqrtisqrt(n)) + 1):
if n % x == 0:
factors factors1.addappend(x)
factors factors2.addappend(n // x)
x += 1
return sorted(factors)
if x * x == n:
 
factors1.append(x)
>>> for i in (45, 53, 64): print( "%i: factors: %s" % (i, factor(i)) )
factors1.extend(reversed(factors2))
return factors1
 
for i in 45, 53, 64:
print("%i: factors: %s" % (i, factor(i)))</syntaxhighlight><pre>
45: factors: [1, 3, 5, 9, 15, 45]
53: factors: [1, 53]
64: factors: [1, 2, 4, 8, 16, 32, 64]</syntaxhighlightpre>
 
More efficient when factoring many numbers:
Line 5,488 ⟶ 5,882:
r += [a*b for a in r for b in e]
return r</syntaxhighlight>
<syntaxhighlight lang="qb64">
'Task
'Compute the factors of a positive integer.
 
'These factors are the positive integers by which the number being factored can be divided to yield a positive integer result.
Dim Dividendum As Integer, Index As Integer
Randomize Timer
Dividendum = Int(Rnd * 1000) + 1
Print " Dividendum: "; Dividendum
Index = Int(Dividendum / 2)
print "Divisors: ";
While Index > 0
If Dividendum Mod Index = 0 Then Print Index; " ";
Index = Index - 1
Wend
End
</syntaxhighlight>
 
=={{header|Quackery}}==
 
<code>isqrtsqrt+</code> is defined at [[Isqrt (integer square root) of X#Quackery]]. It returns the integer square root and remainder (i.e. the square root of 11 is 3 remainder 2, because three squared plus two equals eleven.) If the number is a perfect square the remainder is zero. This is used to remove a duplicate factor from the list of factors which is generated when finding the factors of a perfect square.
 
The nest editing at the end of the definition (i.e. the code after the <code>drop</code> on a line by itself) removes a duplicate factor if there is one, and arranges the factors in ascending numerical order at the same time.
 
<syntaxhighlight lang="text"> [ 1[] swap
[dup 2dupsqrt+ <0 not= whiledip
2 << again ]
0
[ over 1 > while
dip [ 2 >> 2dup - ]
dup 1 >> unrot -
dup 0 < iff drop
else
[ 2swap nip
rot over + ]
again ] nip swap ] is isqrt ( n --> n n )
 
[ [] swap
dup isqrt 0 = dip
[ times
[ dup i^ 1+ /mod iff
Line 5,532 ⟶ 5,896:
rot join
i^ 1+ join swap ]
drop
dup size 2 / split ]
if [ -1 split drop ]
swap join ] is factors ( n --> [ )
Line 5,639 ⟶ 6,003:
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang="raku" line>sub factors (Int $n) { (1..$n).grep($n %% *) }</syntaxhighlight>
 
=={{header|REALbasic}}==
<syntaxhighlight lang="vb">Function factors(num As UInt64) As UInt64()
'This function accepts an unsigned 64 bit integer as input and returns an array of unsigned 64 bit integers
Dim result() As UInt64
Dim iFactor As UInt64 = 1
While iFactor <= num/2 'Since a factor will never be larger than half of the number
If num Mod iFactor = 0 Then
result.Append(iFactor)
End If
iFactor = iFactor + 1
Wend
result.Append(num) 'Since a given number is always a factor of itself
Return result
End Function</syntaxhighlight>
 
=={{header|Red}}==
Line 5,680 ⟶ 6,029:
print mold/flat sort factors num
]</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Factors 120>>;
}
 
Factors {
s.N = <Factors (s.N 1)>;
(s.N s.D), <Compare s.N <* s.D s.D>>: '-' = ;
(s.N s.D), <Divmod s.N s.D>: {
(s.D) 0 = s.D;
(s.F) 0 = s.D <Factors (s.N <+ 1 s.D>)> s.F;
(s.X) s.Y = <Factors (s.N <+ 1 s.D>)>;
};
};</syntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 8 10 12 15 20 24 30 40 60 120</pre>
 
=={{header|Relation}}==
Line 6,209 ⟶ 6,575:
next
</syntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ → n
≪ { } DUP 1 n √ '''FOR''' d
'''IF''' n d MOD NOT
'''THEN'''
d + n d /
'''IF''' DUP d ≠ '''THEN''' ROT + SWAP '''ELSE''' DROP '''END'''
'''END'''
'''NEXT'''
SWAP +
≫ ≫
'FACTS' STO
 
45 FACTS
53 FACTS
64 FACTS
{{out}}
<pre>
3: { 1 3 5 9 15 45 }
2: { 1 53 }
1: { 1 2 4 8 16 32 64 }
</pre>
 
=={{header|Ruby}}==
Line 6,255 ⟶ 6,645:
[1, 2, 4, 5, 10, 20, 25, 50, 100]
</pre>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">PRINT "Factors of 45 are ";factorlist$(45)
PRINT "Factors of 12345 are "; factorlist$(12345)
END
function factorlist$(f)
DIM L(100)
FOR i = 1 TO SQR(f)
IF (f MOD i) = 0 THEN
L(c) = i
c = c + 1
IF (f <> i^2) THEN
L(c) = (f / i)
c = c + 1
END IF
END IF
NEXT i
s = 1
while s = 1
s = 0
for i = 0 to c-1
if L(i) > L(i+1) and L(i+1) <> 0 then
t = L(i)
L(i) = L(i+1)
L(i+1) = t
s = 1
end if
next i
wend
FOR i = 0 TO c-1
factorlist$ = factorlist$ + STR$(L(i)) + ", "
NEXT
end function</syntaxhighlight>
 
{{out}}
<pre>Factors of 45 are 1, 3, 5, 9, 15, 45,
Factors of 12345 are 1, 3, 5, 15, 823, 2469, 4115, 12345, </pre>
 
=={{header|Rust}}==
 
<syntaxhighlight lang="rust">fn main() {
assert_eq!(vec![1, 2, 4, 5, 10, 10, 20, 25, 50, 100], factor(100)); // asserts that two expressions are equal to each other
Line 6,324 ⟶ 6,675:
 
=={{header|Sather}}==
 
<syntaxhighlight lang="sather">class MAIN is
 
Line 6,491 ⟶ 6,841:
 
=={{header|Smalltalk}}==
 
Copied from the Python example, but code added to the Integer built in class:
 
<syntaxhighlight lang="smalltalk">Integer>>factors
| a |
Line 6,654 ⟶ 7,002:
{{out}}
<pre><1,2,4,5,10,20,25,50,100></pre>
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">Function Factors(x As Integer) As String
Application.Volatile
Dim i As Integer
Dim cooresponding_factors As String
Factors = 1
corresponding_factors = x
For i = 2 To Sqr(x)
If x Mod i = 0 Then
Factors = Factors & ", " & i
If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors
End If
Next i
If x <> 1 Then Factors = Factors & ", " & corresponding_factors
End Function</syntaxhighlight>
{{out}}
<pre>cell formula is "=Factors(840)"
resultant value is "1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 15, 20, 21, 24, 28, 30, 35, 40, 42, 56, 60, 70, 84, 105, 120, 140, 168, 210, 280, 420, 840"</pre>
 
 
=={{header|Verilog}}==
Line 6,694 ⟶ 7,022:
45 => 1 3 5 9 15 45
</pre>
 
 
=={{header|Wortel}}==
Line 6,711 ⟶ 7,038:
[1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 30 36 40 45 48 60 72 80 90 120 144 180 240 360 720]
]</pre>
 
=={{header|V (Vlang)}}==
{{trans|Ring}}
<syntaxhighlight lang="Zig">
fn main() {
mut arr := []int{len: 100}
mut n, mut j := 45, 0
for i in 1..n + 1 {
if n % i == 0 {
j++
arr[j] = i
}
}
print("Factors of ${n} = ")
for i in 1..j + 1 {print(" ${arr[i]} ")}
}
</syntaxhighlight>
 
{{out}}
<pre>
Factors of 45 = 1 3 5 9 15 45
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Int
 
var a = [11, 21, 32, 45, 67, 96, 159, 723, 1024, 5673, 12345, 32767, 123459, 999997]
System.print("The factors of the following numbers are:")
for (e in a) SystemFmt.print("%(Fmt.d(6$6d => $n", e)), => %(Int.divisors(e))")</syntaxhighlight>
{{out}}
<pre>
Line 6,830 ⟶ 7,179:
57097 = 57097
</pre>
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">
sub printFactors(n)
if n < 1 then return 0 : fi
print n, " =>",
for i = 1 to n / 2
if mod(n, i) = 0 then print i, " "; : fi
next i
print n
end sub
 
printFactors(11)
printFactors(21)
printFactors(32)
printFactors(45)
printFactors(67)
printFactors(96)
print
end
</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
 
=={{header|zkl}}==
Line 6,871 ⟶ 7,193:
L(L(1,45),L(3,15),L(5,9))
</pre>
 
=={{header|ZX Spectrum Basic}}==
{{trans|AWK}}
<syntaxhighlight lang="zxbasic">10 INPUT "Enter a number or 0 to exit: ";n
20 IF n=0 THEN STOP
30 PRINT "Factors of ";n;": ";
40 FOR i=1 TO n
50 IF FN m(n,i)=0 THEN PRINT i;" ";
60 NEXT i
70 DEF FN m(a,b)=a-INT (a/b)*b</syntaxhighlight>
2,093

edits