Perfect numbers: Difference between revisions
Content added Content deleted
({{header|Liberty BASIC}}) |
(→{{header|Go}}: added validation and output) |
||
Line 304: | Line 304: | ||
# [ 6, 28, 496, 8128 ]</lang> |
# [ 6, 28, 496, 8128 ]</lang> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
<lang go>package main |
|||
{{incorrect|Go|This program apparently pre-loads known perfect numbers into an array and checks to see if the input matches one of them. The spirit of this exercise is to compute whether or not the given integer is perfect.}} |
|||
⚫ | |||
import "fmt" |
|||
⚫ | |||
⚫ | |||
// following function satisfies the task, returning true for all |
|||
⚫ | |||
// perfect numbers representable in the argument type |
|||
⚫ | |||
⚫ | |||
⚫ | |||
switch n { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
return false |
return false |
||
} |
|||
// validation |
|||
func main() { |
|||
for n := int64(1); ; n++ { |
|||
if isPerfect(n) != computePerfect(n) { |
|||
panic("bug") |
|||
⚫ | |||
if n%1e3 == 0 { |
|||
fmt.Println("tested", n) |
|||
} |
|||
} |
|||
} |
|||
func computePerfect(n int64) bool { |
|||
var sum int64 |
|||
for i := int64(1); i < n; i++ { |
|||
⚫ | |||
sum += i |
|||
} |
|||
} |
|||
return sum == n |
|||
}</lang> |
}</lang> |
||
Output: |
|||
<pre> |
|||
tested 1000 |
|||
tested 2000 |
|||
tested 3000 |
|||
... |
|||
</pre> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |