Factors of a Mersenne number: Difference between revisions

Added Easylang
m (syntax highlighting fixup automation)
(Added Easylang)
(3 intermediate revisions by 3 users not shown)
Line 1,082:
=={{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}}==
Line 2,739 ⟶ 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}}==
Line 3,492 ⟶ 3,602:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="go">import math
Line 3,579 ⟶ 3,689:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Conv, Fmt
 
var trialFactor = Fn.new { |base, exp, mod|
2,054

edits