Factors of an integer: Difference between revisions

add Refal
(add Refal)
 
(18 intermediate revisions by 10 users not shown)
Line 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,081 ⟶ 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,104 ⟶ 1,100:
call printFactors(67)
call printFactors(96)
end</syntaxhighlight>
end
</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|BBC BASIC}}===
Line 1,138 ⟶ 1,129:
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}}===
Line 1,173 ⟶ 1,206:
96 => 1 2 3 4 6 8 12 16 24 32 48 96
</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>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|FutureBasic}}===
Line 1,247 ⟶ 1,258:
 
HandleEvents</syntaxhighlight>
{{out}}
 
<pre>Factors of 25 are: 1, 5, 25
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
Line 1,259 ⟶ 1,268:
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>
 
</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,284 ⟶ 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,362 ⟶ 1,394:
end function</syntaxhighlight>
{{out}}
 
<syntaxhighlight lang="lb">Start finding all factors of 10677106534462215678539721403561279:
10677106534462215678539721403561279 = 29269^1 * 32579^1 * 98731^2 * 104729^3
Line 1,481 ⟶ 1,514:
{{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,493 ⟶ 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,511 ⟶ 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]]
 
Line 1,533 ⟶ 1,574:
{{out}}
3 runs.
<pre>ENTER AN INTEGER:1
1</pre>
ENTER AN INTEGER:1
<pre>ENTER AN INTEGER:60
1
1 2 3 4 5 6 10 12 15 20 30 60</pre>
</pre>
<pre>ENTER AN INTEGER:-22222
<pre>
1 2 41 82 271 542 11111 22222</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}}===
Line 1,569 ⟶ 1,604:
EndIf</syntaxhighlight>
{{out}}
<pre> Enter integer to factorize: 96
<pre>
1 2 3 4 6 8 12 16 24 32 48 96</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
'Task
'Compute the factors of a positive integer.
 
Line 1,590 ⟶ 1,622:
Index = Index - 1
Wend
End</syntaxhighlight>
End
</syntaxhighlight>
 
==={{header|QBasic}}===
Line 1,642 ⟶ 1,673:
NEXT
END SUB</syntaxhighlight>
 
{{out}}
<pre> Gimme a number? 17
Gimme a number? 17
1 , 17
Gimme a number? 12345
Line 1,653 ⟶ 1,682:
Gimme a number? 32766
1 , 2 , 3 , 6 , 43 , 86 , 127 , 129 , 254 , 258 , 381 , 762 , 5461 , 10922 ,
16383 , 32766</pre>
 
</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}}===
Line 1,672 ⟶ 1,710:
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasicbasic">PRINT "Factors of 45 are ";factorlist$(45)
PRINT "Factors of 12345 are "; factorlist$(12345)
END
functionFUNCTION factorlist$(f)
DIM L(100)
FOR i = 1 TO SQR(f)
Line 1,689 ⟶ 1,727:
NEXT i
s = 1
whileWHILE s = 1
s = 0
forFOR i = 0 toTO c-1
ifIF L(i) > L(i+1) andAND L(i+1) <> 0 thenTHEN
t = L(i)
L(i) = L(i+1)
L(i+1) = t
s = 1
endEND ifIF
nextNEXT i
WEND
wend
FOR i = 0 TO c-1
factorlist$ = factorlist$ + STR$(L(i)) + ", "
NEXT
endEND functionFUNCTION</syntaxhighlight>
{{out}}
<pre>Factors of 45 are 1, 3, 5, 9, 15, 45,
Line 1,744 ⟶ 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
for i = 1 toIF REMAINDER(n, /i) 2= 0 THEN PRINT i;
NEXT i
if remainder(n, i) = 0 then print i;
nextPRINT in
END SUB
print n
end sub
 
callCALL printfactors(11)
callCALL printfactors(21)
callCALL printfactors(32)
callCALL printfactors(45)
callCALL printfactors(67)
callCALL printfactors(96)
END</syntaxhighlight>
print
end
</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|VBA}}===
Line 1,791 ⟶ 1,821:
<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
sub printFactors(n)
ifprint 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
Line 1,811 ⟶ 1,867:
printFactors(96)
print
end</syntaxhighlight>
end
</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|ZX Spectrum Basic}}===
{{trans|AWK}}
<syntaxhighlight lang="zxbasicbasic">10 INPUT "Enter a number or 0 to exit: ";n
20 IF n=0 THEN STOP
30 PRINT "Factors of ";n;": ";
Line 2,857 ⟶ 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 3,676 ⟶ 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 4,650 ⟶ 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,746 ⟶ 5,885:
=={{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,770 ⟶ 5,896:
rot join
i^ 1+ join swap ]
drop
dup size 2 / split ]
if [ -1 split drop ]
swap join ] is factors ( n --> [ )
Line 5,903 ⟶ 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,895 ⟶ 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>
2,093

edits