Go find some prime numbers!

So a few friends keep bugging me that I need to learn their wonderful hipster language Go, finally I relented this evening and poked around in it for about 45 minutes to create my tried and true primes code that I can almost do in my sleep now. Its ugly and nowhere near as fast as I had it running in C++ back in the day but its kinda cool that I could get it done in about an hour from first writing a line of code in Go. I honestly don’t know if I will ever use the language as a main language in any projects but its the first time I have touched a compiled language in probably the better part of 5 years, these days I do about 105% of my things in Python or Bash when there are command line tools to do most of what I want.

package main

import "fmt"

func main() {
        var Limit int = 10000
        for j := 2; j <= Limit; j++ {
                if j % 2 == 0 {
                        // not prime, we care not!
                } else {
                        // lets do the test loop thing
                        var sLimit int = j / 2

                        var Multiples int = 0
                        for jj := 1; jj <= sLimit; jj++ {
                                if j % jj == 0 {
                                        Multiples++
                                } else {
                                        // no multiples here boss
                                }
                        }
                        if Multiples == 1 {
                                fmt.Println(j, "is probably a prime")
                        }
                }
        }
Close Bitnami banner
Bitnami