Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,24 @@
PROGRAM LL_TEST
!$DOUBLE
PROCEDURE LUCAS_LEHMER(P%->RES)
LOCAL I%,MP,SN
IF P%=2 THEN RES%=TRUE EXIT PROCEDURE END IF
IF (P% AND 1)=0 THEN RES%=FALSE EXIT PROCEDURE END IF
MP=2^P%-1
SN=4
FOR I%=3 TO P% DO
SN=SN^2-2
SN-=(MP*INT(SN/MP))
END FOR
RES%=(SN=0)
END PROCEDURE
BEGIN
PRINT("Mersenne Primes:")
FOR P%=2 TO 23 DO
LUCAS_LEHMER(P%->RES%)
IF RES% THEN PRINT("M";P%) END IF
END FOR
END PROGRAM

View file

@ -0,0 +1,19 @@
(require 'bigint)
(define (mersenne-prime? odd-prime: p)
(define mp (1- (expt 2 p)))
(define s #4)
(for [(i (in-range 3 (1+ p)))]
(set! s (% (- (* s s) 2) mp)))
(when (zero? s) (printf "M%d" p)))
;; run it in the background
(require 'tasks)
(define LP (primes 10000)) ; list of candidate primes
(define (mp-task LP)
(mersenne-prime? (first LP))
(rest LP)) ;; return next state
(task-run (make-task mp-task LP))
→ M3 M5 M7 M13 M17 M19 M31 M61 M89 M107 M127 M521 M607 M1279 M2203 M2281

View file

@ -0,0 +1,53 @@
' version 18-09-2015
' compile with: fbc -s console
#Ifndef TRUE ' define true and false for older freebasic versions
#Define FALSE 0
#Define TRUE Not FALSE
#EndIf
Function mul_mod(a As ULongInt, b As ULongInt, modulus As ULongInt) As ULongInt
' returns a * b mod modulus
Dim As ULongInt x , y = a ' a mod modulus, but a is already smaller then modulus
While b > 0
If (b And 1) = 1 Then
x = (x + y) Mod modulus
End If
y = (y Shl 1) Mod modulus
b = b Shr 1
Wend
Return x
End Function
Function LLT(p As UInteger) As Integer
Dim As ULongInt s = 4, m = 1
m = m Shl p : m = m - 1 ' m = 2 ^ p - 1
For i As Integer = 2 To p - 1
s = mul_mod(s, s, m) - 2
Next
If s = 0 Then Return TRUE Else Return FALSE
End Function
' ------=< MAIN >=------
Dim As UInteger p
Print
' M2 can not be tested, we start with 3
for p = 3 To 63
If LLT(p) = TRUE Then Print " M";Str(p);
Next
Print
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,82 @@
' version 18-09-2015
' compile with: fbc -s console
#Include Once "gmp.bi"
#Macro init_big_int (a)
Dim As Mpz_ptr a = Allocate( Len(__mpz_struct))
Mpz_init(a)
#EndMacro
' ------=< MAIN >=------
Const As UInteger max = 12000 ' 230 sec., 10000 about 125 sec.
Dim As UInteger p, x
Dim As Byte sieve(max)
Dim As String buffer = Space(Len(Str(max))+1)
init_big_int(m)
init_big_int(s)
init_big_int(r)
' sieve to find the primes
' remove even numbers except 2
For p = 4 To Sqr(max) Step 2
sieve(p) = 1
Next
For p = 3 To Sqr(max) Step 2
For x = p * p To max Step p * 2
sieve(x) = 1
Next
Next
' exception: the test will not work for p = 2
For p = 3 To max Step 2 ' odd numbers only
If sieve(p) = 1 Then Continue For
Mpz_set_ui(s, 4) ' s(0) = 4
Mpz_set_ui(m, 1) ' set m to 1
Mpz_mul_2exp(m, m, p) ' m = m shl p = 2 ^ p
Mpz_sub_ui(m, m, 1) ' m = m - 1 = 2 ^ p - 1
For x = 2 To p - 1
Mpz_mul(s, s, s) ' s = s * s
Mpz_sub_ui(s, s, 2) ' s = s - 2
' Mpz_fdiv_r(s, s, m) ' s = s mod m
If Mpz_sgn(s) < 0 Then
Mpz_add(s, s ,m)
Else
Mpz_tdiv_r_2exp(r, s, p)
Mpz_tdiv_q_2exp(s, s, p)
Mpz_add(s, s, r)
End If
If (Mpz_cmp(s, m) >= 0) Then Mpz_sub(s, s, m)
Next
'If Mpz_cmp_ui(s, 0) = 0 Then
' LSet buffer = Str(p)
' Print "M"; buffer; " is prime"
'End If
If Mpz_cmp_ui(s, 0) = 0 Then
Print "M";Str(p),
End If
Next
Print
Mpz_clear (m) ' cleanup
DeAllocate(m)
Mpz_clear (s)
DeAllocate(s)
Mpz_clear (r)
DeAllocate(r)
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,15 @@
def mersenne( p ) =
if p == 2 then return true
var s = 4
var M = 2^p - 1
repeat p - 2
s = (s*s - 2) mod M
s == 0
import integers.primes
for p <- primes().filter( mersenne ).take( 20 )
println( 'M' + p )

View file

@ -0,0 +1,24 @@
import math
proc isPrime(a: int): bool =
if a == 2: return true
if a < 2 or a mod 2 == 0: return false
for i in countup(3, int sqrt(float a), 2):
if a mod i == 0:
return false
return true
proc isMersennePrime(p: int): bool =
if p == 2: return true
let mp = (1'i64 shl p) - 1
var s = 4'i64
for i in 3 .. p:
s = (s * s - 2) mod mp
result = s == 0
let upb = int((log2 float int64.high) / 2)
echo " Mersenne primes:"
for p in 2 .. upb:
if isPrime(p) and isMersennePrime(p):
stdout.write " M",p
echo ""

View file

@ -0,0 +1,7 @@
%%HP: T(3)A(R)F(.); ; ASCII transfer header
\<< DUP LN DUP \pi * 4 SWAP / 1 + UNROT / * IP 2 { 2 } ROT 2 SWAP ; input n; n := Int(n/ln(n)*(1 + 4/(pi*ln(n)))), p:=2; (n ~ number of primes less then n, pi used here only as a convenience), 2 is assumed to be the 1st elemente in the list
START SWAP NEXTPRIME DUP UNROT DUP 2 SWAP ^ 1 - 4 PICK3 2 - 1 SWAP ; for i := 2 to n, p := nextprime; s := 4; m := 2^p - 1;
START SQ 2 - OVER MOD ; for j := 1 to p - 2; s := s^2 mod m;
NEXT NIP NOT { + } { DROP } IFTE ; next j; if s = 0 then add p to the list else discard p;
NEXT NIP ; next i;
\>>

View file

@ -0,0 +1,16 @@
see "Mersenne Primes :" + nl
for p = 2 to 18
if lucasLehmer(p) see "M" + p + nl ok
next
func lucasLehmer p
i = 0 mp = 0 sn = 0
if p = 2 return true ok
if (p and 1) = 0 return false ok
mp = pow(2,p) - 1
sn = 4
for i = 3 to p
sn = pow(sn,2) - 2
sn -= (mp * floor(sn / mp))
next
return (sn=0)

View file

@ -0,0 +1,16 @@
func is_mersenne_prime(p) {
p == 2 && return(true);
var s = 4
var mp = (2**p - 1)
(p-2).times {
s = (s.expmod(2, mp) - 2)
s < 0 && (s += mp)
}
s == 0
}
Inf.times { |n|
n.is_prime ->
&& is_mersenne_prime(n) ->
&& say "M#{n}"
}