Factors of a Mersenne number: Difference between revisions

Added Easylang
(Add 8086 assembly)
(Added Easylang)
(20 intermediate revisions by 13 users not shown)
Line 59:
* &nbsp; [https://www.youtube.com/watch?v=SNwvJ7psoow Computers in 1948: 2<sup>127</sup> - 1] <br> &nbsp; &nbsp; &nbsp; (Note: &nbsp; This video is no longer available because the YouTube account associated with this video has been terminated.)
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2 {R 1B}
I a < 2 | a % 2 == 0 {R 0B}
L(i) (3 .. Int(sqrt(a))).step(2)
I a % i == 0
R 0B
R 1B
 
F m_factor(p)
V max_k = 16384 I/ p
L(k) 0 .< max_k
V q = 2 * p * k + 1
I !is_prime(q)
L.continue
E I q % 8 != 1 & q % 8 != 7
L.continue
E I pow(2, p, q) == 1
R q
R 0
 
V exponent = Int(input(‘Enter exponent of Mersenne number: ’))
I !is_prime(exponent)
print(‘Exponent is not prime: #.’.format(exponent))
E
V factor = m_factor(exponent)
I factor == 0
print(‘No factor found for M#.’.format(exponent))
E
print(‘M#. has a factor: #.’.format(exponent, factor))</syntaxhighlight>
 
{{out}}
<pre>
Enter exponent of Mersenne number: 929
M929 has a factor: 13007
</pre>
 
=={{header|8086 Assembly}}==
 
<langsyntaxhighlight lang="asm">P: equ 929 ; P for 2^P-1
cpu 8086
bits 16
Line 155 ⟶ 194:
factor: db "Found factor: $"
prcnt: dw 2 ; Amount of primes currently in list
primes: dw 2, 3 ; List of primes to be extended</langsyntaxhighlight>
 
{{out}}
Line 165 ⟶ 204:
Use of bitwise operations
(TM (Test under Mask), SLA (Shift Left Arithmetic),SRA (Shift Right Arithmetic)).
<syntaxhighlight lang="text">* Factors of a Mersenne number 11/09/2015
MERSENNE CSECT
USING MERSENNE,R15
Line 254 ⟶ 293:
PG DS CL24 buffer
YREGS
END MERSENNE</langsyntaxhighlight>
{{out}}
<pre>
Line 262 ⟶ 301:
=={{header|Ada}}==
mersenne.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
-- reuse Is_Prime from [[Primality by Trial Division]]
with Is_Prime;
Line 329 ⟶ 368:
Ada.Text_IO.Put_Line ("is not a Mersenne number");
end;
end Mersenne;</langsyntaxhighlight>
 
{{out}}
Line 341 ⟶ 380:
<!-- {{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
Compiles, but I couldn't maxint not in library, works with manually entered maxint, bits width. Leaving some issue with newline -->
<langsyntaxhighlight lang="algol68">MODE ISPRIMEINT = INT;
PR READ "prelude/is_prime.a68" PR;
 
Line 391 ⟶ 430:
FI
 
END</langsyntaxhighlight>
Example:
<pre>
Line 397 ⟶ 436:
M +929 has a factor: +13007
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">mersenneFactors: function [q][
if not? prime? q -> print "number not prime!"
r: new q
while -> r > 0
-> shl 'r 1
d: new 1 + 2 * q
while [true][
i: new 1
p: new r
while [p <> 0][
i: new (i * i) % d
if p < 0 -> 'i * 2
if i > d -> 'i - d
shl 'p 1
]
if? i <> 1 -> 'd + 2 * q
else -> break
]
print ["2 ^" q "- 1 = 0 ( mod" d ")"]
]
 
mersenneFactors 929</syntaxhighlight>
 
{{out}}
 
<pre>2 ^ 929 - 1 = 0 ( mod 13007 )</pre>
 
=={{header|AutoHotkey}}==
ahk [http://www.autohotkey.com/forum/viewtopic.php?t=44657&postdays=0&postorder=asc&start=144 discussion]
<langsyntaxhighlight lang="autohotkey">MsgBox % MFact(27) ;-1: 27 is not prime
MsgBox % MFact(2) ; 0
MsgBox % MFact(3) ; 0
Line 455 ⟶ 523:
y := i&1 ? mod(y*z,m) : y, z := mod(z*z,m), i >>= 1
Return y
}</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> PRINT "A factor of M929 is "; FNmersenne_factor(929)
PRINT "A factor of M937 is "; FNmersenne_factor(937)
END
Line 495 ⟶ 563:
ENDWHILE
= Y%
</syntaxhighlight>
</lang>
{{out}}
<pre>A factor of M929 is 13007
Line 501 ⟶ 569:
 
=={{header|Bracmat}}==
<langsyntaxhighlight Bracmatlang="bracmat">( ( modPow
= square P divisor highbit log 2pow
. !arg:(?P.?divisor)
Line 562 ⟶ 630:
| out$"no divisors found"
)
);</langsyntaxhighlight>
{{out}}
<pre>found some divisors of 2^!P-1 : 13007 and 348890248924938259750454781163390930305120269538278042934009621348894657205785
Line 570 ⟶ 638:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">int isPrime(int n){
if (n%2==0) return n==2;
if (n%3==0) return n==3;
Line 593 ⟶ 661:
else break;
} while(1);
printf("2^%d - 1 = 0 (mod %d)\n", q, d);}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace prog
Line 641 ⟶ 709:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cstdint>
#include <vector>
 
typedef uint64_t integer;
Line 652 ⟶ 719:
integer bit_count(integer n) {
integer count = 0;
whilefor (; n > 0); {count++)
n >>= 1;
++count;
}
return count;
}
Line 661 ⟶ 726:
integer mod_pow(integer p, integer n) {
integer square = 1;
for (integer bits = bit_count(p); bits > 0; square %= n) {
while (bits > 0) {square *= square;
squareif =(p square& *(1 square;<< --bits))
if ((p & (1 << --bits)) != 0)
square <<= 1;
square %= n;
}
return square;
Line 676 ⟶ 739:
if (n % 2 == 0)
return n == 2;
for (integer p = 3; p * p <= n; p += 2) {
if (n % p == 0)
return false;
}
return true;
}
 
integer find_mersenne_factor(integer p) {
for (integer k = 0, q = 1;;) {
integer q = 2 * ++k * p + 1;
if ((q % 8 == 1 || q % 8 == 7) && mod_pow(p, q) == 1 && is_prime(q))
for (;;) {
++k return q;
q = 2 * k * p + 1;
if (q % 8 == 1 || q % 8 == 7) {
if (mod_pow(p, q) == 1 && is_prime(q))
return q;
}
}
return 0;
Line 698 ⟶ 755:
 
int main() {
std::cout << find_mersenne_factor(929) << '\n'std::endl;
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 710 ⟶ 767:
{{trans|Python}}
 
<langsyntaxhighlight lang="lisp">(ns mersennenumber
(:gen-class))
 
Line 768 ⟶ 825:
:let [s (-main p)]]
(println s))
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 796 ⟶ 853:
{{trans|Ruby}}
 
<langsyntaxhighlight lang="coffeescript">mersenneFactor = (p) ->
limit = Math.sqrt(Math.pow(2,p) - 1)
k = 1
Line 823 ⟶ 880:
 
checkMersenne(prime) for prime in ["2","3","4","5","7","11","13","17","19","23","29","31","37","41","43","47","53","929"]
</syntaxhighlight>
</lang>
 
<pre>M2 = 2^2-1 is prime
Line 847 ⟶ 904:
=={{header|Common Lisp}}==
{{trans|Maxima}}
<langsyntaxhighlight lang="lisp">(defun mersenne-fac (p &aux (m (1- (expt 2 p))))
(loop for k from 1
for n = (1+ (* 2 k p))
Line 853 ⟶ 910:
finally (return n)))
 
(print (mersenne-fac 929))</langsyntaxhighlight>
 
{{out}}
Line 862 ⟶ 919:
We can use a primality test from the [[Primality by Trial Division#Common_Lisp|Primality by Trial Division]] task.
 
<langsyntaxhighlight lang="lisp">(defun primep (n)
"Is N prime?"
(and (> n 1)
(or (= n 2) (oddp n))
(loop for i from 3 to (isqrt n) by 2
never (zerop (rem n i)))))</langsyntaxhighlight>
 
Specific to this task, we define modulo-power and mersenne-prime-p.
 
<langsyntaxhighlight lang="lisp">(defun modulo-power (base power modulus)
(loop with square = 1
for bit across (format nil "~b" power)
Line 889 ⟶ 946:
(primep q)
(= 1 (modulo-power 2 power q)))
(return (values nil q)))))</langsyntaxhighlight>
 
We can run the same tests from the [[#Ruby|Ruby]] entry.
Line 919 ⟶ 976:
=={{header|Crystal}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">require "big"
 
def prime?(n) # P3 Prime Generator primality test
Line 961 ⟶ 1,018:
 
(2..53).each { |p| check_mersenne(p) if prime?(p) }
check_mersenne 929</langsyntaxhighlight>
 
{{out}}
Line 983 ⟶ 1,040:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.traits;
 
ulong mersenneFactor(in ulong p) pure nothrow @nogc {
Line 1,019 ⟶ 1,076:
void main() {
writefln("Factor of M929: %d", 929.mersenneFactor);
}</langsyntaxhighlight>
{{out}}
<pre>Factor of M929: 13007</pre>
 
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Factors_of_a_Mersenne_number#Pascal Pascal].
 
=={{header|EasyLang}}==
{{trans|C++}}
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func bit_count n .
while n > 0
n = bitshift n -1
cnt += 1
.
return cnt
.
func mod_pow p n .
square = 1
bits = bit_count p
while bits > 0
square *= square
bits -= 1
if bitand p bitshift 1 bits > 0
square = bitshift square 1
.
square = square mod n
.
return square
.
func mersenne_factor p .
while 1 = 1
k += 1
q = 2 * k * p + 1
if (q mod 8 = 1 or q mod 8 = 7) and mod_pow p q = 1 and isprim q = 1
return q
.
.
.
print mersenne_factor 929
</syntaxhighlight>
{{out}}
<pre>
13007
</pre>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; M = 2^P - 1 , P prime
;; look for a prime divisor q such as : q < √ M, q = 1 or 7 modulo 8, q = 1 + 2kP
Line 1,048 ⟶ 1,157:
→ #t
 
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule Mersenne do
def mersenne_factor(p) do
limit = :math.sqrt(:math.pow(2, p) - 1)
Line 1,087 ⟶ 1,196:
 
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,929]
|> Enum.each(fn p -> Mersenne.check_mersenne(p) end)</langsyntaxhighlight>
 
{{out}}
Line 1,114 ⟶ 1,223:
The modpow function is not my original. This is a translation of python, more or less.
 
<langsyntaxhighlight lang="erlang">
-module(mersene2).
-export([prime/1,modpow/3,mf/1]).
Line 1,157 ⟶ 1,266:
false -> divisors(N, C-1)
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,172 ⟶ 1,281:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit interpolate io kernel locals
math math.bits math.functions math.primes sequences ;
IN: rosetta-code.mersenne-factors
Line 1,203 ⟶ 1,312:
[ [I No factor found for M${}.I] ] if* nl ;
 
929 test-mersenne</langsyntaxhighlight>
{{out}}
<pre>
Line 1,210 ⟶ 1,319:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: prime? ( odd -- ? )
3
begin 2dup dup * >=
Line 1,242 ⟶ 1,351:
 
929 factor-mersenne . \ 13007
4423 factor-mersenne . \ 0</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">PROGRAM EXAMPLE
IMPLICIT NONE
INTEGER :: exponent, factor
Line 1,297 ⟶ 1,406:
Mfactor = 0
END FUNCTION
END PROGRAM EXAMPLE</langsyntaxhighlight>
{{out}}
M929 has a factor: 13007
Line 1,303 ⟶ 1,412:
=={{header|FreeBASIC}}==
{{trans|C}}
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function isPrime(n As Integer) As Boolean
Line 1,347 ⟶ 1,456:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,367 ⟶ 1,476:
2^97 - 1 = 0 (mod 11447)
2^929 - 1 = 0 (mod 13007)
</pre>
 
=={{header|Frink}}==
Frink has built-in routines for iterating through prime numbers and modular exponentiation. The following program will find all of the factors of the number given enough runtime.
<syntaxhighlight lang="frink">for p = primes[]
if modPow[2, 929, p] - 1 == 0
println[p]</syntaxhighlight>
{{out}}
<pre>
13007
</pre>
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">MersenneSmallFactor := function(n)
local k, m, d;
if IsPrime(n) then
Line 1,401 ⟶ 1,520:
 
FactorsInt(2^101-1);
# [ 7432339208719, 341117531003194129 ]</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,484 ⟶ 1,603:
}
return int32(pow)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,496 ⟶ 1,615:
Using David Amos module Primes [https://web.archive.org/web/20121130222921/http://www.polyomino.f2s.com/david/haskell/codeindex.html] for prime number testing:
 
<langsyntaxhighlight lang="haskell">import Data.List
import HFM.Primes (isPrime)
import Control.Monad
Line 1,508 ⟶ 1,627:
map (succ.(2*m*)). enumFromTo 1 $ m `div` 2
bs = int2bin m
pm n b = 2^b*n*n</langsyntaxhighlight>
 
<langsyntaxhighlight lang="haskell">*Main> trialfac 929
[13007]</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,517 ⟶ 1,636:
 
The following works in both languages:
<langsyntaxhighlight lang="unicon">procedure main(A)
p := integer(A[1]) | 929
write("M",p," has a factor ",mfactor(p))
Line 1,548 ⟶ 1,667:
}
return
end</langsyntaxhighlight>
 
Sample runs:
Line 1,561 ⟶ 1,680:
=={{header|J}}==
 
<langsyntaxhighlight lang="j">trialfac=: 3 : 0
qs=. (#~8&(1=|+.7=|))(#~1&p:)1+(*(1x+i.@<:@<.)&.-:)y
qs#~1=qs&|@(2&^@[**:@])/ 1,~ |.#: y
)</langsyntaxhighlight>
 
{{out|Examples}}
<langsyntaxhighlight lang="j">trialfac 929
13007</langsyntaxhighlight>
<syntaxhighlight lang ="j">trialfac 44497</langsyntaxhighlight>
Empty output --> No factors found.
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
 
Line 1,642 ⟶ 1,761:
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,669 ⟶ 1,788:
=={{header|JavaScript}}==
 
<langsyntaxhighlight lang="javascript">function mersenne_factor(p){
var limit, k, q
limit = Math.sqrt(Math.pow(2,p) - 1)
Line 1,709 ⟶ 1,828:
f = mersenne_factor(p)
console.log(f == null ? "prime" : "composite with factor "+f)
}</langsyntaxhighlight>
 
<pre>
Line 1,721 ⟶ 1,840:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># v0.6
 
using Primes
Line 1,741 ⟶ 1,860:
if mf != -1 println("M$i = ", mf, " × ", (big(2) ^ i - 1) ÷ mf)
else println("M$i is prime") end
end</langsyntaxhighlight>
 
{{out}}
Line 1,759 ⟶ 1,878:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun isPrime(n: Int): Boolean {
Line 1,805 ⟶ 1,924:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,828 ⟶ 1,947:
 
=={{header|Lingo}}==
<langsyntaxhighlight Lingolang="lingo">on modPow (b, e, m)
bits = getBits(e)
sq = 1
Line 1,850 ⟶ 1,969:
end repeat
return bits
end</langsyntaxhighlight>
 
<langsyntaxhighlight Lingolang="lingo">repeat with i = 2 to the maxInteger
if modPow(2, 929, i)=1 then
put "M929 has a factor: " & i
exit repeat
end if
end repeat</langsyntaxhighlight>
 
{{out}}
Line 1,864 ⟶ 1,983:
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Believe it or not, this type of test runs faster in Mathematica than the squaring version described above.
 
<syntaxhighlight lang="mathematica">For[i = 2, i < Prime[1000000], i = NextPrime[i],
<lang mathematica>
For[i = 2, i < Prime[1000000], i = NextPrime[i],
If[Mod[2^44497, i] == 1,
Print["divisible by "<>i]]]; Print["prime test passed; call Lucas and Lehmer"]</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">mersenne_fac(p) := block([m: 2^p - 1, k: 1],
while mod(m, 2 * k * p + 1) # 0 do k: k + 1,
2 * k * p + 1
Line 1,879 ⟶ 1,997:
 
mersenne_fac(929);
/* 13007 */</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight lang="nim">import math
 
proc isPrime(a: int): bool =
Line 1,908 ⟶ 2,026:
if i != 1: d += 2 * q
else: break
echo "2^",q," - 1 = 0 (mod ",d,")"</langsyntaxhighlight>
{{out}}
<pre>2^929 - 1 = 0 (mod 13007)</pre>
Line 1,917 ⟶ 2,035:
(GNU Octave has a <code>isprime</code> built-in test)
 
<langsyntaxhighlight lang="octave">% test a bit; lsb is 1 (like built-in bit* ops)
function b = bittst(n, p)
b = bitand(n, 2^(p-1)) > 0;
Line 1,950 ⟶ 2,068:
endfunction
 
printf("%d\n", Mfactor(929));</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
This version takes about 15 microseconds to find a factor of 2<sup>929</sup> &minus; 1.
<langsyntaxhighlight lang="parigp">factorMersenne(p)={
forstep(q=2*p+1,sqrt(2)<<(p\2),2*p,
[1,0,0,0,0,0,1][q%8] && Mod(2, q)^p==1 && return(q)
Line 1,960 ⟶ 2,078:
1<<p-1
};
factorMersenne(929)</langsyntaxhighlight>
 
This implementation seems to be broken:
<langsyntaxhighlight lang="parigp">TM(p) = local(status=1, i=1, len=0, S=0);{
printp("Test TM \t...");
S=2*p+1;
Line 1,982 ⟶ 2,100:
);
return(status);
}</langsyntaxhighlight>
 
=={{header|Pascal}}==
{{trans|Fortran}}
<langsyntaxhighlight lang="pascal">program FactorsMersenneNumber(input, output);
 
function isPrime(n: longint): boolean;
Line 2,069 ⟶ 2,187:
else
writeln('M', exponent, ' (2**', exponent, ' - 1) has the factor: ', factor);
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,078 ⟶ 2,196:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use utf8;
 
Line 2,138 ⟶ 2,256:
print $f? "M$m = $x = $q × @{[$x / $q]}\n"
: "M$m = $x is prime\n";
}</langsyntaxhighlight>
{{out}}
<pre>M2 = 3 is prime
Line 2,153 ⟶ 2,271:
 
Following the task introduction, this uses GMP's modular exponentiation and simple probable prime test for the small numbers, then looks for small factors before doing a Lucas-Lehmer test. For ranges above about p=2000, looking for small factors this way saves time (the amount of testing should be adjusted based on the input size and platform -- this example just uses a fixed amount). Note as well that the Lucas-Lehmer test shown here is ignoring the large speedup we can get by optimizing the modulo operation, but that's a different task.
<langsyntaxhighlight lang="perl">use Math::GMP;
 
# Use GMP's simple probable prime test.
Line 2,182 ⟶ 2,300:
print "M$p is ", is_mersenne_prime($p) ? "prime" : "composite", "\n";
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,215 ⟶ 2,333:
=={{header|Phix}}==
Translation/Amalgamation of BBC BASIC, D, and Go
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function modpow(atom x, atom n, atom m)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
atom i = n,
<span style="color: #008080;">function</span> <span style="color: #000000;">modpow</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
y = 1,
<span style="color: #004080;">atom</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span>
z = x
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
while i do
<span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span>
if and_bits(i,1) then
<span style="color: #008080;">while</span> <span style="color: #000000;">i</span> <span style="color: #008080;">do</span>
y = mod(y*z,m)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">*</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
z = mod(z*z,m)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
i = floor(i/2)
<span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">*</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
return y
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">y</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function mersenne_factor(integer p)
if not is_prime(p) then return -1 end if
<span style="color: #008080;">function</span> <span style="color: #000000;">mersenne_factor</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
atom limit = sqrt(power(2,p))-1
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
integer k = 1
<span style="color: #004080;">atom</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">))-</span><span style="color: #000000;">1</span>
while 1 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
atom q = 2*p*k + 1
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
if q>=limit then exit end if
<span style="color: #004080;">atom</span> <span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">p</span><span style="color: #0000FF;">*</span><span style="color: #000000;">k</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span>
if find(mod(q,8),{1,7})
<span style="color: #008080;">if</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
and is_prime(q)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">})</span>
and modpow(2,p,q)=1 then
<span style="color: #008080;">and</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">)</span>
return q
<span style="color: #008080;">and</span> <span style="color: #000000;">modpow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">q</span>
k += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #000000;">k</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
return 0
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">0</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sequence tests = {11, 23, 29, 37, 41, 43, 47, 53, 59, 67, 71, 73, 79, 83, 97, 929, 937}
for i=1 to length(tests) do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">23</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">29</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">37</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">41</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">43</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">47</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">53</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">59</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">67</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">71</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">73</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">79</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">83</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">97</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">929</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">937</span><span style="color: #0000FF;">}</span>
integer ti = tests[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
printf(1,"A factor of M%d is %d\n",{ti,mersenne_factor(ti)})
<span style="color: #004080;">integer</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
end for</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"A factor of M%d is %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mersenne_factor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{Out}}
<pre>
Line 2,275 ⟶ 2,396:
{{trans|D}}
Requires bcmath
<langsyntaxhighlight lang="php">echo 'M929 has a factor: ', mersenneFactor(929), '</br>';
 
function mersenneFactor($p) {
Line 2,296 ⟶ 2,417:
}
return true;
}</langsyntaxhighlight>
 
{{out}}
Line 2,302 ⟶ 2,423:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de **Mod (X Y N)
(let M 1
(loop
Line 2,332 ⟶ 2,453:
(prime? Q)
(= 1 (**Mod 2 P Q)) )
Q ) ) ) )</langsyntaxhighlight>
{{out}}
<pre>: (for P (2 3 4 5 7 11 13 17 19 23 29 31 37 41 43 47 53 929)
Line 2,361 ⟶ 2,482:
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">
mersenne_factor(P, F) :-
prime(P),
Line 2,389 ⟶ 2,510:
N mod D =\= 0,
D2 is D + A, prime(N, D2, As).
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,410 ⟶ 2,531:
=={{header|Python}}==
 
<langsyntaxhighlight lang="python">def is_prime(number):
return True # code omitted - see Primality by Trial Division
 
Line 2,434 ⟶ 2,555:
print "No factor found for M%d" % exponent
else:
print "M%d has a factor: %d" % (exponent, factor)</langsyntaxhighlight>
 
{{out|Example}}
Line 2,443 ⟶ 2,564:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
Line 2,464 ⟶ 2,585:
 
(mersenne-factor 929)
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="racket">
13007
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2015.12}}
<lang perl6>my @primes = 2, 3, -> $n is copy {
repeat { $n += 2 } until $n %% none do for @primes -> $p {
last if $p > sqrt($n);
$p;
}
$n;
} ... *;
multi factors(1) { 1 }
multi factors(Int $remainder is copy) {
gather for @primes -> $factor {
if $factor * $factor > $remainder {
take $remainder if $remainder > 1;
last;
}
while $remainder %% $factor {
take $factor;
last if ($remainder div= $factor) === 1;
}
}
}
 
sub is_prime($x) { (state %){$x} //= factors($x) == 1 }
 
<syntaxhighlight lang="raku" line>sub mtest($bits, $p) {
my @bits = $bits.base(2).comb;
loop (my $sq = 1; @bits; $sq %= $p) {
$sq *×= $sq;
$sq += $sq if 1 == @bits.shift;
}
$sq == 1;
Line 2,507 ⟶ 2,604:
 
for flat 2 .. 60, 929 -> $m {
next unless is_primeis-prime($m);
my $f = 0;
my $x = 2**$m - 1;
my $q;
for 1..* -> $k {
$q = 2 *× $k *× $m + 1;
next unless $q % 8 == 1|7 or is_primeis-prime($q);
last if $q *× $q > $x or $f = mtest($m, $q);
}
 
say $f ?? "M$m = $x\n\t= $q × { $x div $q }"
!! "M$m = $x is prime";
}</langsyntaxhighlight>
{{out}}
<pre>M2 = 3 is prime
Line 2,554 ⟶ 2,651:
 
This REXX version automatically adjusts the &nbsp; '''numeric digits''' &nbsp; to whatever is needed.
<langsyntaxhighlight lang="rexx">/*REXX program uses exponent─and─mod operator to test possible Mersenne numbers. */
numeric digits 20 /*this will be increased if necessary. */
parse arg N spec /*obtain optional arguments from the CL*/
Line 2,606 ⟶ 2,703:
end /*until*/
if sq==1 then return q /*Not a prime? Return a factor.*/
end /*k*/</langsyntaxhighlight>
Program note: &nbsp; the &nbsp; '''iSqrt''' &nbsp; function computes the integer square root of a non-negative integer without using any floating point, just integers.
 
Line 2,643 ⟶ 2,740:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Factors of a Mersenne number
 
Line 2,685 ⟶ 2,782:
end
return y
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,691 ⟶ 2,788:
A factor of M937 is 28111
</pre>
 
=={{header|RPL}}==
{{works with|HP|48}}
<code>PRIM?</code> is defined at [[Primality by trial division#RPL|Primality by trial division]]
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ SWAP R→B → quotient power
≪ 2 power B→R LN 2 LN / FLOOR ^ R→B
1
'''WHILE''' OVER B→R '''REPEAT'''
SQ
'''IF''' OVER power AND B→R '''THEN''' DUP + '''END'''
quotient MOD
SWAP SR SWAP
'''END''' SWAP DROP
≫ ≫ '<span style="color:blue">MODPOW</span>' STO
≪ 2 OVER ^ 1 - √ 0 → power max k
≪ 1
'''WHILE''' 'k' INCR 2 * 1 + DUP max ≤ '''REPEAT'''
'''IF''' { 1 7 } OVER 8 MOD POS THEN
'''IF''' DUP <span style="color:blue">PRIM?</span> THEN
'''IF''' power OVER <span style="color:blue">MODPOW</span> 1 == '''THEN'''
SWAP max 'k' STO '''END'''
'''END END'''
DROP
'''END''' DROP
≫ '<span style="color:blue">MFACT</span>' STO
|
<span style="color:blue">MODPOW</span> ''( power quotient → remainder )''
create top-bit mask
square = 1
while mask is not zero
square *= square
if unmasked bit = 1 then square += square
square = square mod quotient
shift mask right
clean stack
return square
<span style="color:blue">MFACT</span> ''( N → factor ) ''
factor = 1
while 2k+1 ≤ sqrt(M(N))
if 2k+1 mod 8 is equal to 1 or 7
if 2k+1 prime
is 2K+1 a factor of M(N) ?
if yes, exit loop
return factor
|}
929 <span style="color:blue">MFACT</span>
{{out}}
<pre>
1: 13007
</pre>
Factor found in 69 minutes on a 4-bit HP-48SX calculator.
 
=={{header|Ruby}}==
{{works with|Ruby|1.9.3+}}
<langsyntaxhighlight lang="ruby">require 'prime'
 
def mersenne_factor(p)
Line 2,727 ⟶ 2,885:
 
Prime.each(53) { |p| check_mersenne p }
check_mersenne 929</langsyntaxhighlight>
 
{{out}}
Line 2,750 ⟶ 2,908:
=={{header|Rust}}==
{{trans|C++}}
<langsyntaxhighlight lang="rust">fn bit_count(mut n: usize) -> usize {
let mut count = 0;
while n > 0 {
Line 2,812 ⟶ 2,970:
fn main() {
println!("{}", find_mersenne_factor(929));
}</langsyntaxhighlight>
 
{{out}}
Line 2,823 ⟶ 2,981:
 
===Full-blown version===
<syntaxhighlight lang="scala">
<lang Scala>
/** Find factors of a Mersenne number
*
Line 2,862 ⟶ 3,020:
(primes takeWhile (_ <= 97)) ++ List(929, 937) foreach { p => { // Needs some intermediate results for nice formatting
val nMersenne = mersenne(p);
val lit = fs"${nMersenne}%30d"
val preAmble = f"${s"M${p}"}%4s = 2^$p%03d - 1 = ${lit}%s"
 
Line 2,883 ⟶ 3,041:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:40ex;overflow:scroll"> M2 = 2^002 - 1 = 3 is a Mersenne prime number. (63 msec)
Line 2,935 ⟶ 3,093:
This works with PLT Scheme, other implementations only need to change the inclusion.
 
<langsyntaxhighlight lang="scheme">
#lang scheme
 
Line 2,957 ⟶ 3,115:
(= 1 (modpow p i))))
i))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,969 ⟶ 3,127:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: isPrime (in integer: number) is func
Line 3,032 ⟶ 3,190:
begin
writeln("Factor of M929: " <& mersenneFactor(929));
end func;</langsyntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/math.htm#isPrime isPrime],
Line 3,043 ⟶ 3,201:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func mtest(b, p) {
var bits = b.base(2).digits
for (var sq = 1; bits; sq %= p) {
Line 3,063 ⟶ 3,221:
say (f ? "M#{m} is composite with factor #{q}"
: "M#{m} is prime")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,088 ⟶ 3,246:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension BinaryInteger {
Line 3,150 ⟶ 3,308:
 
print(mFactor(exp: 929)!)
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,158 ⟶ 3,316:
=={{header|Tcl}}==
For <code>primes::is_prime</code> see [[Prime decomposition#Tcl]]
<langsyntaxhighlight lang="tcl">proc int2bits {n} {
binary scan [binary format I1 $n] B* binstring
return [split [string trimleft $binstring 0] ""]
Line 3,202 ⟶ 3,360:
} else {
puts "no factor found for M$exp"
}</langsyntaxhighlight>
 
=={{header|TI-83 BASIC}}==
Line 3,208 ⟶ 3,366:
{{works with|TI-83 BASIC|TI-84Plus 2.55MP}}
The program uses the new remainder function from OS 2.53MP, if not available it can be replaced by:
<langsyntaxhighlight lang="ti83b">remainder(A,B) equivalent to iPart(B*fPart(A/B))</langsyntaxhighlight>Due to several problems, no Goto has been used. As a matter of fact the version is clearer.
<langsyntaxhighlight lang="ti83b">Prompt Q
1→K:0→T
While K≤2^20 and T=0
Line 3,241 ⟶ 3,399:
End
If T=0:0→F
Disp Q,F</langsyntaxhighlight>
{{in}}
<pre>
Line 3,254 ⟶ 3,412:
 
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">Print "A factor of M929 is "; FUNC(_FNmersenne_factor(929))
Print "A factor of M937 is "; FUNC(_FNmersenne_factor(937))
 
Line 3,307 ⟶ 3,465:
Next
 
Return (d@)</langsyntaxhighlight>
{{out}}
<pre>A factor of M929 is 13007
Line 3,316 ⟶ 3,474:
=={{header|VBScript}}==
{{trans|REXX}}
<langsyntaxhighlight lang="vb">' Factors of a Mersenne number
for i=1 to 59
z=i
Line 3,378 ⟶ 3,536:
loop
testM=0
end function</langsyntaxhighlight>
{{out}}
<pre>
Line 3,403 ⟶ 3,561:
{{trans|BBC BASIC}}
{{works with|Visual Basic|VB6 Standard}}
<langsyntaxhighlight lang="vb">Sub mersenne()
Dim q As Long, k As Long, p As Long, d As Long
Dim factor As Long, i As Long, y As Long, z As Long
Line 3,438 ⟶ 3,596:
okfactor:
Debug.Print "M" & q, "factor=" & factor
End Sub</langsyntaxhighlight>
{{Out}}
<pre>
M47 factor=2351
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="go">import math
const qlimit = int(2e8)
fn main() {
mtest(31)
mtest(67)
mtest(929)
}
fn mtest(m int) {
// the function finds odd prime factors by
// searching no farther than sqrt(N), where N = 2^m-1.
// the first odd prime is 3, 3^2 = 9, so M3 = 7 is still too small.
// M4 = 15 is first number for which test is meaningful.
if m < 4 {
println("$m < 4. M$m not tested.")
return
}
flimit := math.sqrt(math.pow(2, f64(m)) - 1)
mut qlast := 0
if flimit < qlimit {
qlast = int(flimit)
} else {
qlast = qlimit
}
mut composite := []bool{len: qlast+1}
sq := int(math.sqrt(f64(qlast)))
loop:
for q := int(3); ; {
if q <= sq {
for i := q * q; i <= qlast; i += q {
composite[i] = true
}
}
q8 := q % 8
if (q8 == 1 || q8 == 7) && mod_pow(2, m, q) == 1 {
println("M$m has factor $q")
return
}
for {
q += 2
if q > qlast {
break loop
}
if !composite[q] {
break
}
}
}
println("No factors of M$m found.")
}
// base b to power p, mod m
fn mod_pow(b int, p int, m int) int {
mut pow := i64(1)
b64 := i64(b)
m64 := i64(m)
mut bit := u32(30)
for 1<<bit&p == 0 {
bit--
}
for {
pow *= pow
if 1<<bit&p != 0 {
pow *= b64
}
pow %= m64
if bit == 0 {
break
}
bit--
}
return int(pow)
}</syntaxhighlight>
{{out}}
<pre>
No factors of M31 found.
M67 has factor 193707721
M929 has factor 13007
</pre>
 
Line 3,448 ⟶ 3,689:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Conv, Fmt
 
var trialFactor = Fn.new { |base, exp, mod|
Line 3,483 ⟶ 3,724:
System.print("prime")
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,511 ⟶ 3,752:
=={{header|zkl}}==
{{trans|EchoLisp}}
<langsyntaxhighlight lang="zkl">var [const] BN=Import("zklBigNum"); // libGMP
 
// M = 2^P - 1 , P prime
Line 3,527 ⟶ 3,768:
}
False
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">m_divisor(929).println(); // 13007
m_divisor(4423).println(); // False
(BN(2).pow(4423) - 1).probablyPrime().println(); // True</langsyntaxhighlight>
{{out}}
<pre>
2,041

edits