mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
689 B
53 lines
689 B
5 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/01-edu/z01"
|
||
|
)
|
||
|
|
||
|
func isPrime(nb int) bool {
|
||
|
if nb <= 0 || nb == 1 {
|
||
|
return false
|
||
|
}
|
||
|
if nb <= 3 {
|
||
|
return true
|
||
5 years ago
|
}
|
||
|
if nb%2 == 0 || nb%3 == 0 {
|
||
5 years ago
|
return false
|
||
|
}
|
||
|
|
||
|
i := 5
|
||
|
for i*i <= nb {
|
||
|
if nb%i == 0 || nb%(i+2) == 0 {
|
||
|
return false
|
||
|
}
|
||
5 years ago
|
i += 6
|
||
5 years ago
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
if len(os.Args) != 2 {
|
||
|
z01.PrintRune('0')
|
||
|
z01.PrintRune('\n')
|
||
|
} else {
|
||
|
argument, _ := strconv.Atoi(os.Args[1])
|
||
|
|
||
|
if argument < 0 {
|
||
|
z01.PrintRune('0')
|
||
|
z01.PrintRune('\n')
|
||
|
} else {
|
||
|
result := 0
|
||
|
for ; argument >= 0; argument-- {
|
||
5 years ago
|
if isPrime(argument) {
|
||
|
result += argument
|
||
5 years ago
|
}
|
||
|
}
|
||
|
fmt.Println(result)
|
||
|
}
|
||
|
}
|
||
|
}
|