mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-13 16:51:17 +00:00
44 lines
557 B
Go
44 lines
557 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
func printPrimes(max int) {
|
|
for n := 2; n <= max; n++ {
|
|
if n == 2 {
|
|
fmt.Println(n)
|
|
continue
|
|
}
|
|
|
|
if n%2 == 0 {
|
|
continue
|
|
}
|
|
|
|
isPrime := true
|
|
for i := 3; i*i <= n; i++ {
|
|
if n%i == 0 {
|
|
isPrime = false
|
|
break
|
|
}
|
|
}
|
|
if isPrime {
|
|
fmt.Println(n)
|
|
}
|
|
}
|
|
}
|
|
|
|
// don't edit below this line
|
|
|
|
func test(max int) {
|
|
fmt.Printf("Primes up to %v:\n", max)
|
|
printPrimes(max)
|
|
fmt.Println("===============================================================")
|
|
}
|
|
|
|
func main() {
|
|
test(10)
|
|
test(20)
|
|
test(30)
|
|
}
|