Factors of an integer: Difference between revisions
(Flag for correction, add clarifying WP link) |
(Expand the description of the task) |
||
Line 2: | Line 2: | ||
[[Category:Arithmetic operations]] |
[[Category:Arithmetic operations]] |
||
[[Category:Mathematical_operations]] |
[[Category:Mathematical_operations]] |
||
Compute the [[wp:Divisor|factors]] of a number. The factors of a positive integer are those positive integers by which it can be divided to yield a positive integer result (though the concepts function correctly for zero and negative integers, the set of factors of zero is has countably infinite members, and the factors of negative integers can be obtained from the factors of related positive numbers without difficulty; this task does not require handling of either of these cases). Note that even prime numbers will have at least two factors; ‘1’ and themselves. |
|||
Compute the [[wp:Divisor|factors]] of a number. |
|||
See also |
See also: |
||
* [[Prime decomposition]] |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
Revision as of 12:17, 17 August 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:
Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses
Compute the factors of a number. The factors of a positive integer are those positive integers by which it can be divided to yield a positive integer result (though the concepts function correctly for zero and negative integers, the set of factors of zero is has countably infinite members, and the factors of negative integers can be obtained from the factors of related positive numbers without difficulty; this task does not require handling of either of these cases). Note that even prime numbers will have at least two factors; ‘1’ and themselves.
See also:
Clojure
<lang lisp>(defn factors [n] (filter #(zero? (rem n %)) (range 1 n)))
(print (factors 45))</lang>
(1 3 5 9 15)
Python
<lang python>>>> def factors(n): return [i for i in range(1,n//2) if not n%i] + [n]
>>> factors(45) [1, 3, 5, 9, 15, 45]</lang>
Ruby
<lang ruby>class Integer
def factors() (1..self).select { |n| (self % n).zero? } end
end p 45.factors</lang>
[1, 3, 5, 9, 15, 45]
As we only have to loop up to , we can write <lang ruby>class Integer
def factors() 1.upto(Math.sqrt(self)).select {|i| (self % i).zero?}.inject([]) do |f, i| f << i f << self/i unless i == self/i f end.sort end
end [45, 53, 64].each {|n| p n.factors}</lang> output
[1, 3, 5, 9, 15, 45] [1, 53] [1, 2, 4, 8, 16, 32, 64]
Tcl
<lang tcl>proc factors {n} {
set factors {} for {set i 1} {$i <= sqrt($n)} {incr i} { if {$n % $i == 0} { lappend factors $i [expr {$n / $i}] } } return [lsort -unique -integer $factors]
} puts [factors 64] puts [factors 45] puts [factors 53]</lang> output
1 2 4 8 16 32 64 1 3 5 9 15 45 1 53