Skip to content

Commit 0217421

Browse files
Merge pull request #11 from bradleyjkemp/master
Standardise file naming convention
2 parents 89ce82b + d996ca8 commit 0217421

File tree

10 files changed

+136
-0
lines changed

10 files changed

+136
-0
lines changed

other/prime_numbers.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"os"
7+
"strconv"
8+
"github.com/douglasmakey/golang-algorithms-/utils"
9+
"time"
10+
)
11+
12+
// Define struct
13+
type numberResult struct {
14+
number int64
15+
isPrime bool
16+
}
17+
18+
// Define functions
19+
20+
// isPrime: validate N number is prime
21+
func isPrime(n int64) bool {
22+
var i, limit int64
23+
if n <= 1 {
24+
return false
25+
}
26+
27+
if n == 2 {
28+
return true
29+
}
30+
31+
if math.Mod(float64(n), 2) == 0 {
32+
return false
33+
}
34+
35+
36+
limit = int64(math.Ceil(math.Sqrt(float64(n))))
37+
for i = 3; i <= limit; i += 2 {
38+
if math.Mod(float64(n), float64(i)) == 0 {
39+
return false
40+
}
41+
}
42+
return true
43+
}
44+
45+
// createNrAndValidate: Receive number and validate if is prime, send channel this same
46+
func createNrAndValidate(n int64, c chan numberResult) {
47+
48+
result := new(numberResult)
49+
result.number = n
50+
result.isPrime = isPrime(n)
51+
c <- *result
52+
}
53+
54+
func initGoCalculations(min int64, max int64, c chan numberResult) {
55+
var i int64
56+
for i = min; i <= max; i++ {
57+
go createNrAndValidate(i, c)
58+
}
59+
}
60+
61+
func primesInRange(min int64, max int64) (primeArr []int64) {
62+
defer utils.TimeTrack(time.Now(), "primesInRange")
63+
64+
// Create channels and defer close
65+
c := make(chan numberResult)
66+
defer close(c)
67+
68+
// Execute N goroutines in range number
69+
go initGoCalculations(min, max, c)
70+
71+
for i := min; i <= max; i++ {
72+
// Receive numberResult
73+
r := <-c
74+
if r.isPrime {
75+
primeArr = append(primeArr, r.number)
76+
}
77+
}
78+
return
79+
}
80+
81+
func main() {
82+
// Receive arguments min max
83+
min, _ := strconv.ParseInt(os.Args[1], 10, 64)
84+
max, _ := strconv.ParseInt(os.Args[2], 10, 64)
85+
fmt.Println(primesInRange(min, max))
86+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

sorts/insertion_sort.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
arr:= [9]int{2,1,4,3,5,9,7,6,8}
7+
8+
for out:=1; out<len(arr); out++{
9+
temp:=arr[out]
10+
in := out
11+
12+
for;in>0 && arr[in-1]>=temp;in--{
13+
arr[in] = arr[in-1]
14+
}
15+
arr[in] = temp
16+
}
17+
18+
for sortedvals:= range arr{
19+
fmt.Println(sortedvals)
20+
}
21+
}
File renamed without changes.
File renamed without changes.

sorts/selection_sort.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
4+
import "fmt"
5+
6+
7+
func main() {
8+
arr := [5]int{11, 12, 31, 4, 1}
9+
fmt.Println("Initial array is:", arr)
10+
fmt.Println("")
11+
12+
var min int = 0
13+
var tmp int = 0
14+
15+
for i := 0; i < len(arr); i++ {
16+
min = i
17+
for j := i + 1; j < len(arr); j++ {
18+
if arr[j] < arr[min] {
19+
min = j
20+
}
21+
}
22+
23+
tmp = arr[i]
24+
arr[i] = arr[min]
25+
arr[min] = tmp
26+
}
27+
28+
fmt.Println("Sorted array: ", arr)
29+
}
File renamed without changes.

0 commit comments

Comments
 (0)