File: main.go

package info (click to toggle)
golang-github-tscholl2-siec 0.0~git20210707.9bdfc48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 360 kB
  • sloc: makefile: 3
file content (61 lines) | stat: -rw-r--r-- 1,266 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main

import (
	"fmt"
	"math"
	"math/big"
	"sort"
	"sync"
)

func main() {
	// build results
	var results [][2]*big.Int
	M := new(big.Int).Lsh(one, 256)
	var mutex = &sync.Mutex{}
	var waitGroup sync.WaitGroup
	waitGroup.Add(255)
	for j := uint(0); j < 255; j++ {
		go func(j uint) {
			defer waitGroup.Done()
			M2 := new(big.Int).Set(M)
			M2.Sub(M2, new(big.Int).Lsh(one, j))
			var q, t *big.Int
			q = M2
			for {
				q, t = nextSiec(q)
				N := new(big.Int).Add(q, big.NewInt(1))
				N.Sub(N, t)
				N2 := new(big.Int).Add(q, big.NewInt(1))
				N2.Add(N2, t)
				if true || N.ProbablyPrime(25) || N2.ProbablyPrime(25) {
					mutex.Lock()
					results = append(results, [2]*big.Int{q, t})
					mutex.Unlock()
					return
				}
			}
		}(j)
	}
	waitGroup.Wait()
	// rank results
	sort.Slice(results, func(i int, j int) bool {
		n1 := results[i][0].BitLen()
		c1 := popCount(results[i][0])
		n2 := results[j][0].BitLen()
		c2 := popCount(results[j][0])
		return math.Abs(float64(n1-2*int(c1))) < math.Abs(float64(n2-2*int(c2)))
	})
	// print
	for _, r := range results {
		fmt.Printf("q = %x\ns = %d\nt = %x\n", r[0], popCount(r[0]), r[1])
	}
}

func popCount(a *big.Int) (c uint) {
	n := a.BitLen()
	for i := 0; i <= n; i++ {
		c += a.Bit(i)
	}
	return
}