Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Perfect-numbers/Go/perfect-numbers.go
Normal file
36
Task/Perfect-numbers/Go/perfect-numbers.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func computePerfect(n int64) bool {
|
||||
var sum int64
|
||||
for i := int64(1); i < n; i++ {
|
||||
if n%i == 0 {
|
||||
sum += i
|
||||
}
|
||||
}
|
||||
return sum == n
|
||||
}
|
||||
|
||||
// following function satisfies the task, returning true for all
|
||||
// perfect numbers representable in the argument type
|
||||
func isPerfect(n int64) bool {
|
||||
switch n {
|
||||
case 6, 28, 496, 8128, 33550336, 8589869056,
|
||||
137438691328, 2305843008139952128:
|
||||
return true
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue