How to resolve the algorithm Factors of a Mersenne number step by step in the Bracmat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Factors of a Mersenne number step by step in the Bracmat programming language

Table of Contents

Problem Statement

A Mersenne number is a number in the form of 2P-1. If P is prime, the Mersenne number may be a Mersenne prime (if P is not prime, the Mersenne number is also not prime). In the search for Mersenne prime numbers it is advantageous to eliminate exponents by finding a small factor before starting a, potentially lengthy, Lucas-Lehmer test. There are very efficient algorithms for determining if a number divides 2P-1 (or equivalently, if 2P mod (the number) = 1). Some languages already have built-in implementations of this exponent-and-mod operation (called modPow or similar). The following is how to implement this modPow yourself: For example, let's compute 223 mod 47. Convert the exponent 23 to binary, you get 10111. Starting with square = 1, repeatedly square it. Remove the top bit of the exponent, and if it's 1 multiply square by the base of the exponentiation (2), then compute square modulo 47. Use the result of the modulo from the last step as the initial value of square in the next step: Since 223 mod 47 = 1, 47 is a factor of 2P-1. (To see this, subtract 1 from both sides: 223-1 = 0 mod 47.) Since we've shown that 47 is a factor, 223-1 is not prime. Further properties of Mersenne numbers allow us to refine the process even more. Any factor q of 2P-1 must be of the form 2kP+1, k being a positive integer or zero. Furthermore, q must be 1 or 7 mod 8. Finally any potential factor q must be prime. As in other trial division algorithms, the algorithm stops when 2kP+1 > sqrt(N). These primality tests only work on Mersenne numbers where P is prime. For example, M4=15 yields no factors using these techniques, but factors into 3 and 5, neither of which fit 2kP+1.

Using the above method find a factor of 2929-1 (aka M929)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Factors of a Mersenne number step by step in the Bracmat programming language

Source code in the bracmat programming language

( ( modPow
  =   square P divisor highbit log 2pow
    .   !arg:(?P.?divisor)
      & 1:?square
      & 2\L!P:#%?log+?
      & 2^!log:?2pow
      &   whl
        ' (     mod
              $ (   ( div$(!P.!2pow):1&2
                    | 1
                    )
                  * !square^2
                . !divisor
                )
            : ?square
          & mod$(!P.!2pow):?P
          & 1/2*!2pow:~/:?2pow
          )
      & !square
  )
& ( isPrime
  =   incs nextincs primeCandidate nextPrimeCandidate quotient
    .     1 1 2 2 (4 2 4 2 4 6 2 6:?incs)
        : ?nextincs
      & 1:?primeCandidate
      & ( nextPrimeCandidate
        =   ( !nextincs:&!incs:?nextincs
            | 
            )
          & !nextincs:%?inc ?nextincs
          & !inc+!primeCandidate:?primeCandidate
        )
      &   whl
        ' ( (!nextPrimeCandidate:?divisor)^2:~>!arg
          & !arg*!divisor^-1:?quotient:/
          )
      & !quotient:/
  )
& ( Factors-of-a-Mersenne-Number
  =   P k candidate bignum
    .   !arg:?P
      & 2^!P+-1:?bignum
      & 0:?k
      &   whl
        ' ( 2*(1+!k:?k)*!P+1:?candidate
          & !candidate^2:~>!bignum
          & ( ~(mod$(!candidate.8):(1|7))
            | ~(isPrime$!candidate)
            | modPow$(!P.!candidate):?mp:~1
            )
          )
      & !mp:1
      & (!candidate.(2^!P+-1)*!candidate^-1)
  )
& (   Factors-of-a-Mersenne-Number$929:(?divisorA.?divisorB)
    &   out
      $ ( str
        $ ("found some divisors of 2^" !P "-1 : " !divisorA " and " !divisorB)
        )
  | out$"no divisors found"
  )
);

  

You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the Prolog programming language
You may also check:How to resolve the algorithm File size distribution step by step in the Perl programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Maxima programming language
You may also check:How to resolve the algorithm JSON step by step in the Python programming language
You may also check:How to resolve the algorithm Program name step by step in the Phixmonti programming language