Jump to content

Factors of an integer: Difference between revisions

→‎{{header|Sidef}}: mentioned the built-in function
mNo edit summary
(→‎{{header|Sidef}}: mentioned the built-in function)
Line 4,632:
 
=={{header|Sidef}}==
Built-in:
<lang ruby>func factors(n) {
<lang ruby>say divisors(97) #=> [1, 97]
say divisors(2695) #=> [1, 5, 7, 11, 35, 49, 55, 77, 245, 385, 539, 2695]</lang>
 
Trial-division (slow for large n):
 
<lang ruby>func factorsdivisors(n) {
gather {
{ |d|
Line 4,640 ⟶ 4,646:
}
 
for n [53, 64, 32766].each {|n|
say "factorsdivisors(#{n}): #{factorsdivisors(n)}"
}</lang>
{{out}}
<pre>
factorsdivisors(53): [1, 53]
factorsdivisors(64): [1, 2, 4, 8, 16, 32, 64]
factorsdivisors(32766): [1, 2, 3, 6, 43, 86, 127, 129, 254, 258, 381, 762, 5461, 10922, 16383, 32766]
</pre>
 
2,747

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.