Factors of an integer: Difference between revisions
Content added Content deleted
(Task created with Ruby and Clojure implementations.) |
(→{{header|Python}}: (loops up to n/2)) |
||
Line 8: | Line 8: | ||
(print (factors 45))</lang> |
(print (factors 45))</lang> |
||
(1 3 5 9 15) |
(1 3 5 9 15) |
||
=={{header|Python}}== |
|||
<lang python>>>> def factors(n): |
|||
return [i for i in range(1,n//2) if not n%i] |
|||
>>> factors(45) |
|||
[1, 3, 5, 9, 15]</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
Revision as of 12:55, 15 August 2009
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Compute the factors of a number.
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]
>>> factors(45) [1, 3, 5, 9, 15]</lang>
Ruby
<lang ruby>class Integer
def factors() (1..self - 1).select { |n| (self % n).zero? } end
end p 45.factors</lang>
[1, 3, 5, 9, 15]
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