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.}}

<lang go>func isPerfect(n int64) bool {
import "fmt"
for _, p := range []int64{6, 28, 496, 8128, 33550336,

8589869056, 137438691328, 2305843008139952128} {
// following function satisfies the task, returning true for all
if n == p {
// perfect numbers representable in the argument type
return true
func isPerfect(n int64) bool {
}
switch n {
case 6, 28, 496, 8128, 33550336, 8589869056,
137438691328, 2305843008139952128:
return true
}
}
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++ {
if n%i == 0 {
sum += i
}
}
return sum == n
}</lang>
}</lang>
Output:
<pre>
tested 1000
tested 2000
tested 3000
...
</pre>


=={{header|Groovy}}==
=={{header|Groovy}}==