Skip to content

Instantly share code, notes, and snippets.

@luv2code
Last active October 10, 2015 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luv2code/3649847 to your computer and use it in GitHub Desktop.
Save luv2code/3649847 to your computer and use it in GitHub Desktop.
Problem with references?
package main
import "fmt"
func isPrimeDivisible(c int) bool {
prime := 0
for i := 0; i < prime_count; i++ {
prime = primes[i]
if (prime * prime) > c {
return false
}
if (c % prime) == 0 {
fmt.Printf("c mod prime %v, c %v, prime %v \n", (c%prime), c, prime)
return true
}
}
return false
}
var primes = make([]int, 1, 25000)
var prime_count = 1
func main() {
primes[0] = 2
c := 3
fmt.Printf("Primes %v", primes)
for prime_count < 25 && c < 100000{ //25000 {
fmt.Printf("isDivisible? %v\n", isPrimeDivisible(c))
if !isPrimeDivisible(c) {
primes = append(primes, c)
prime_count++
}
c++
}
fmt.Println(primes[prime_count-1])
}
@luv2code
Copy link
Author

luv2code commented Sep 6, 2012

This is supposed to print out the first 25 primes. It doesn't work and I can't figure out why.

@zeebo
Copy link

zeebo commented Sep 6, 2012

Well I looked over it and there are a couple problems:

  1. 1 is not a prime number. You should set primes[0] = 2
  2. You start at c = 1, but for the algorithm to work without duplicates you need c > primes[prime_count-1], so start with c = 3

After that it works just fine.

@luv2code
Copy link
Author

luv2code commented Sep 6, 2012

thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment