tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 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"
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func computePerfect(n int64) bool {
|
||||
var sum int64
|
||||
for i := int64(1); i < n; i++ {
|
||||
if n%i == 0 {
|
||||
sum += i
|
||||
}
|
||||
}
|
||||
return sum == n
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue