Skip to main content
Add interest
Source Link
sargas
  • 245
  • 2
  • 7

I'm interested in solutions involving goroutines, pointers, both, etc.

In Go:

In Go:

I'm interested in solutions involving goroutines, pointers, both, etc.

In Go:

Add comments to clarify code
Source Link
sargas
  • 245
  • 2
  • 7
package main

import (
    "fmt"
    "time"
)

func main() {
    t1 := time.Now()

    // Create slice containing values from 0 to 1 million
    a := []int{}
    for i := 0; i < 1000000; i++ {
        a = append(a, i)
    }

    done := false
    factor := 1
    for !done {
        // Get the next factor to be used as a filter for slice `a` 
        factor = nextPrime(a, factor)

        var modified bool
        // Filtering slice `a`
        for i, v := range a {
            if v%factor == 0 && v > factor {
                a[i] = 0 // This is equivalent to eliminating the value
                modified = true
            }
        }

        if !modified {
            done = true
        }
    }

    var sum int
    for _, v := range a {
        sum += v
    }
    fmt.Println(sum - 1)

    t2 := time.Now()
    fmt.Println("Execution duration:", t2.Sub(t1))
} 

// Doesn't return a prime literaly, but the least value to be used
// to filter out non prime numbers from an array or slice
func nextPrime(a []int, factor int) (newFactor int) {
    for _, v := range a {
        if v > factor {
            newFactor = v
            break
        }
    }
    return
}
package main

import (
    "fmt"
    "time"
)

func main() {
    t1 := time.Now()

    a := []int{}
    for i := 0; i < 1000000; i++ {
        a = append(a, i)
    }

    done := false
    factor := 1
    for !done {
        factor = nextPrime(a, factor)

        var modified bool
        for i, v := range a {
            if v%factor == 0 && v > factor {
                a[i] = 0
                modified = true
            }
        }

        if !modified {
            done = true
        }
    }

    var sum int
    for _, v := range a {
        sum += v
    }
    fmt.Println(sum - 1)

    t2 := time.Now()
    fmt.Println("Execution duration:", t2.Sub(t1))
}

func nextPrime(a []int, factor int) (newFactor int) {
    for _, v := range a {
        if v > factor {
            newFactor = v
            break
        }
    }
    return
}
package main

import (
    "fmt"
    "time"
)

func main() {
    t1 := time.Now()

    // Create slice containing values from 0 to 1 million
    a := []int{}
    for i := 0; i < 1000000; i++ {
        a = append(a, i)
    }

    done := false
    factor := 1
    for !done {
        // Get the next factor to be used as a filter for slice `a` 
        factor = nextPrime(a, factor)

        var modified bool
        // Filtering slice `a`
        for i, v := range a {
            if v%factor == 0 && v > factor {
                a[i] = 0 // This is equivalent to eliminating the value
                modified = true
            }
        }

        if !modified {
            done = true
        }
    }

    var sum int
    for _, v := range a {
        sum += v
    }
    fmt.Println(sum - 1)

    t2 := time.Now()
    fmt.Println("Execution duration:", t2.Sub(t1))
} 

// Doesn't return a prime literaly, but the least value to be used
// to filter out non prime numbers from an array or slice
func nextPrime(a []int, factor int) (newFactor int) {
    for _, v := range a {
        if v > factor {
            newFactor = v
            break
        }
    }
    return
}
Change title to avoid misconceptions
Link
sargas
  • 245
  • 2
  • 7

Sum of primes in given range: in Go vs Ruby

deleted 23 characters in body
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading
request is mainly to review Go
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading
edited tags; edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading
added 108 characters in body
Source Link
RubberDuck
  • 30.9k
  • 6
  • 71
  • 174
Loading
Improve title
Link
sargas
  • 245
  • 2
  • 7
Loading
Source Link
sargas
  • 245
  • 2
  • 7
Loading