package main

import (

"fmt"

)

func printPrimesBaby(max int) {

// start checking from 2, up to the max

for i := 2; i <= max; i++ {

// if it is 2, then it's a prime, print out and continue.

if i == 2 {

fmt.Println(i)

continue

}

// if divides into 2, not a prime. Continue

if i%2 == 0 {

continue

}

// max is bigger than 2? starting from 3 up to the max, seeing if anything divides into it

isItPrime := true

for j := 3; i >= j*j; j++ {

if i%j == 0 {

isItPrime = false

break

}

continue

}

if isItPrime {

fmt.Println(i)

}

}

}

func test(max int) {

fmt.Printf("Primes up to %v:\n", max)

printPrimesBaby(max)

fmt.Println("===============================================================")

}

func main() {

test(10)

test(20)

test(30)

}

Reply to this note

Please Login to reply.

Discussion

No replies yet.