github.com/cznic/mathutil@v0.0.0-20181122101859-297441e03548/ff/main.go (about)

     1  // Copyright (c) jnml. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build ignore
     6  
     7  // Factor Finder - searches for Mersenne number factors of one specific special
     8  // form.
     9  package main
    10  
    11  import (
    12  	"flag"
    13  	"fmt"
    14  	"math/big"
    15  	"runtime"
    16  	"time"
    17  
    18  	"github.com/cznic/mathutil"
    19  )
    20  
    21  const (
    22  	pp  = 1
    23  	pp2 = 10
    24  )
    25  
    26  var (
    27  	_1 = big.NewInt(1)
    28  	_2 = big.NewInt(2)
    29  )
    30  
    31  func main() {
    32  	runtime.GOMAXPROCS(2)
    33  	oClass := flag.Uint64("c", 2, `factor "class" number`)
    34  	oDuration := flag.Duration("d", time.Second, "duration to spend on one class")
    35  	flag.Parse()
    36  	class := *oClass
    37  	for class&1 != 0 {
    38  		class >>= 1
    39  	}
    40  	class = mathutil.MaxUint64(class, 2)
    41  
    42  	for {
    43  		c := time.After(*oDuration)
    44  		factor := big.NewInt(0)
    45  		factor.SetUint64(class)
    46  		exp := big.NewInt(0)
    47  	oneClass:
    48  		for {
    49  			select {
    50  			case <-c:
    51  				break oneClass
    52  			default:
    53  			}
    54  
    55  			exp.Set(factor)
    56  			factor.Lsh(factor, 1)
    57  			factor.Add(factor, _1)
    58  			if !factor.ProbablyPrime(pp) {
    59  				continue
    60  			}
    61  
    62  			if !exp.ProbablyPrime(pp) {
    63  				continue
    64  			}
    65  
    66  			if mathutil.ModPowBigInt(_2, exp, factor).Cmp(_1) != 0 {
    67  				continue
    68  			}
    69  
    70  			if !factor.ProbablyPrime(pp2) {
    71  				continue
    72  			}
    73  
    74  			if !exp.ProbablyPrime(pp2) {
    75  				continue
    76  			}
    77  
    78  			fmt.Printf("%d: %s | M%s (%d bits)\n", class, factor, exp, factor.BitLen())
    79  		}
    80  
    81  		class += 2
    82  	}
    83  }