Perfect numbers: Difference between revisions
Content added Content deleted
m (→{{header|C++}}: fix lang tag; added a works with instead of simple text (i believe this can work also with other compilers anyway)) |
(→{{header|Scheme}}: ++ smalltalk) |
||
Line 307: | Line 307: | ||
(else |
(else |
||
(loop (+ i 1) sum)))))</lang> |
(loop (+ i 1) sum)))))</lang> |
||
=={{header|Smalltalk}}== |
|||
<lang smalltalk>Integer extend [ |
|||
"Translation of the C version; this is faster..." |
|||
isPerfectC [ |tot| tot := 1. |
|||
(2 to: (self sqrt) + 1) do: [ :i | |
|||
(self rem: i) = 0 |
|||
ifTrue: [ |q| |
|||
tot := tot + i. |
|||
q := self // i. |
|||
q > i ifTrue: [ tot := tot + q ] |
|||
] |
|||
]. |
|||
^ tot = self |
|||
] |
|||
"... but this seems more idiomatic" |
|||
isPerfect [ |
|||
^ ( ( ( 2 to: self // 2 + 1) select: [ :a | (self rem: a) = 0 ] ) |
|||
inject: 1 into: [ :a :b | a + b ] ) = self |
|||
] |
|||
].</lang> |
|||
<lang smalltalk>1 to: 9000 do: [ :p | (p isPerfect) ifTrue: [ p printNl ] ]</lang> |