aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-147/pokgopun/README1
-rw-r--r--challenge-147/pokgopun/go/ch-1.go103
-rw-r--r--challenge-147/pokgopun/go/ch-2.go64
3 files changed, 168 insertions, 0 deletions
diff --git a/challenge-147/pokgopun/README b/challenge-147/pokgopun/README
new file mode 100644
index 0000000000..33dfd303a4
--- /dev/null
+++ b/challenge-147/pokgopun/README
@@ -0,0 +1 @@
+Solution by PokGoPun
diff --git a/challenge-147/pokgopun/go/ch-1.go b/challenge-147/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..99dda019e8
--- /dev/null
+++ b/challenge-147/pokgopun/go/ch-1.go
@@ -0,0 +1,103 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "regexp"
+ "strconv"
+)
+
+func main() {
+ l := 20
+ if len(os.Args) > 1 {
+ i, err := strconv.Atoi(os.Args[1])
+ if err != nil {
+ log.Fatal(err)
+ }
+ l = i
+ }
+ ltp := []int{}
+ chk, nxt, _ := primeUtil()
+ re := regexp.MustCompile(`0`)
+ p := 9
+ for i := 0; i < 100; i++ {
+ p = nxt(p)
+ s := []byte(strconv.Itoa(p))
+ if re.MatchString(string(s)) {
+ continue
+ }
+ count := 1
+ for j := 1; j < len(s); j++ {
+ tp, err := strconv.Atoi(string(s[j:]))
+ if err != nil {
+ log.Fatal(err)
+ }
+ if !chk(tp) {
+ break
+ }
+ count++
+ }
+ if count == len(s) {
+ ltp = append(ltp, p)
+ if len(ltp) == l {
+ break
+ }
+ }
+ }
+ //fmt.Println(len(ltp), ltp)
+ fmt.Println(ltp)
+}
+
+func primeUtil() (func(i int) bool, func(i int) int, func()) {
+ m := map[int]bool{}
+ return func(i int) bool {
+ is, ok := m[i]
+ if ok {
+ //fmt.Println("### check prime from cache ###")
+ return is
+ } else {
+ if isPrime(i) {
+ m[i] = true
+ return true
+ }
+ m[i] = false
+ return false
+ }
+ }, func(i int) int {
+ for {
+ i++
+ is, ok := m[i]
+ if ok {
+ //fmt.Println("### check next number from cache ###")
+ if is {
+ return i
+ } else {
+ continue
+ }
+ } else {
+ if isPrime(i) {
+ m[i] = true
+ return i
+ }
+ m[i] = false
+ }
+ }
+ }, func() {
+ fmt.Println("=>", m)
+ }
+}
+func isPrime(n int) bool {
+ //fmt.Println("In isPrime")
+ if n <= 3 {
+ return n > 1
+ } else if n%2 == 0 || n%3 == 0 {
+ return false
+ }
+ for i := 5; i*i <= n; i += 6 {
+ if n%i == 0 || n%(i+2) == 0 {
+ return false
+ }
+ }
+ return true
+}
diff --git a/challenge-147/pokgopun/go/ch-2.go b/challenge-147/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..33eb44387f
--- /dev/null
+++ b/challenge-147/pokgopun/go/ch-2.go
@@ -0,0 +1,64 @@
+package main
+
+import (
+ "fmt"
+ "math"
+)
+
+func main() {
+ i := 0
+ maxIdx := 3_000
+ for {
+ i++
+ if i > maxIdx-1 {
+ break
+ }
+ pi := calPtg(i)
+ j := i
+ for {
+ j++
+ if j > maxIdx {
+ break
+ }
+ pj := calPtg(j)
+ if pj+pi < calPtg(j+1) {
+ break
+ }
+ sumIdx := idxPtg(pj + pi)
+ if sumIdx > 0 {
+ diffIdx := idxPtg(pj - pi)
+ if diffIdx > 0 {
+ fmt.Printf("P(%v) + P(%v) => %v + %v = %v <= P(%v)\n", j, i, pj, pi, pj+pi, sumIdx)
+ fmt.Printf("P(%v) - P(%v) => %v - %v = %v <= P(%v)\n", j, i, pj, pi, pj-pi, diffIdx)
+ fmt.Print("\n")
+ break
+ }
+ }
+ }
+ }
+}
+
+func calPtg(i int) int {
+ return (3*i - 1) * i / 2
+}
+
+// idxPtg check if n is a Pentagon number and return its index if it is, it returns 0 if it is not
+func idxPtg(n int) int {
+ // find approximation of index of Pentagon number to start iterate up to find the actual index
+ // p = i(3i - 1)/2 => 2p = 3i^2 - i => 3i^2 = 2p + i => i^2 = 2*p/3 + i/3 => i ~~ sqrt(2*p/3)
+ i := int(math.Ceil(math.Sqrt(2 * float64(n) / 3)))
+ //fmt.Println("i=", i, "=>p=", n)
+ var p int
+ for {
+ p = (3*i - 1) * i / 2
+ if p >= n {
+ break
+ }
+ i++
+ }
+ //return p - n
+ if p-n == 0 {
+ return i
+ }
+ return 0
+}