Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
21
Task/Perfect-numbers/Smalltalk/perfect-numbers-1.st
Normal file
21
Task/Perfect-numbers/Smalltalk/perfect-numbers-1.st
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
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
|
||||
]
|
||||
].
|
||||
1
Task/Perfect-numbers/Smalltalk/perfect-numbers-2.st
Normal file
1
Task/Perfect-numbers/Smalltalk/perfect-numbers-2.st
Normal file
|
|
@ -0,0 +1 @@
|
|||
1 to: 9000 do: [ :p | (p isPerfect) ifTrue: [ p printNl ] ]
|
||||
Loading…
Add table
Add a link
Reference in a new issue