Wieferich primes: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 22:
=={{header|Ada}}==
{{trans|BASIC256}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Wieferich_Primes is
Line 62:
end loop;
 
end Wieferich_Primes;</langsyntaxhighlight>
{{out}}
<pre>
Line 72:
=={{header|APL}}==
'''Works in:''' [[Dyalog APL]]
<langsyntaxhighlight lang="apl"> ⎕CY 'dfns' ⍝ import dfns namespace
⍝ pco ← prime finder
⍝ nats ← natural number arithmetic (uses strings)
Line 79:
wief 5000
1093 3511
</syntaxhighlight>
</lang>
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">wieferich?: function [n][
and? -> prime? n
-> zero? (dec 2 ^ n-1) % n ^ 2
]
 
print ["Wieferich primes less than 5000:" select 1..5000 => wieferich?]</langsyntaxhighlight>
 
{{out}}
Line 94:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f WIEFERICH_PRIMES.AWK
# converted from FreeBASIC
Line 132:
return(q == 1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 143:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="freebasic">print "Wieferich primes less than 5000: "
for i = 1 to 5000
if isWeiferich(i) then print i
Line 169:
end while
return True
end function</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
Line 175:
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.i isPrime(n)
Protected k
Line 216:
Next i
Input()
CloseConsole()</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
 
==={{header|Run BASIC}}===
<langsyntaxhighlight lang="runbasic">print "Wieferich primes less than 5000: "
for i = 1 to 5000
if isWeiferich(i) then print i
Line 252:
end if
[exit]
end function</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
Line 258:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">print "Wieferich primes less than 5000: "
for i = 2 to 5000
if isWeiferich(i) print i
Line 284:
wend
return True
end sub</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
Line 291:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
Line 360:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Wieferich primes less than 5000:
Line 367:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cstdint>
#include <iostream>
#include <vector>
Line 419:
for (uint64_t p : wieferich_primes(limit))
std::cout << p << '\n';
}</langsyntaxhighlight>
 
{{out}}
Line 430:
=={{header|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 501:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Wieferich primes less that 5000:
Line 509:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Weiferich primes: Nigel Galloway. June 2nd., 2021
primes32()|>Seq.takeWhile((>)5000)|>Seq.filter(fun n->(2I**(n-1)-1I)%(bigint(n*n))=0I)|>Seq.iter(printfn "%d")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 522:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: io kernel math math.functions math.primes prettyprint
sequences ;
 
"Wieferich primes less than 5000:" print
5000 primes-upto [ [ 1 - 2^ 1 - ] [ sq divisor? ] bi ] filter .</langsyntaxhighlight>
{{out}}
<pre>
Line 534:
 
=={{header|fermat}}==
<langsyntaxhighlight lang="fermat">
Func Iswief(p)=Isprime(p)*Divides(p^2, 2^(p-1)-1).
for i=2 to 5000 do if Iswief(i) then !!i fi od
</syntaxhighlight>
</lang>
{{out}}<pre>
1093
Line 545:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
 
Line 599:
 
5000 wieferich_primes
bye</langsyntaxhighlight>
 
{{out}}
Line 609:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#include "isprime.bas"
 
Line 624:
for i as uinteger = 1 to 5000
if iswief(i) then print i
next i</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 652:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 662:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">isPrime :: Integer -> Bool
isPrime n
|n == 2 = True
Line 680:
main = do
putStrLn "Wieferich primes less than 5000:"
print solution</langsyntaxhighlight>
{{out}}
<pre>Wieferich primes less than 5000:
Line 687:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> I.(1&p: * 0=*: | _1+2x^<:) i.5000
1093 3511</langsyntaxhighlight>
 
About 12 times faster:
 
<langsyntaxhighlight Jlang="j"> p: I. (0=*:|_1+2x^<:) I.1 p: i.5000
1093 3511</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">import java.util.*;
 
public class WieferichPrimes {
Line 751:
return result;
}
}</langsyntaxhighlight>
 
{{out}}
Line 764:
 
gojq supports unbounded-precision integer arithmetic and so is up to this task.
<langsyntaxhighlight lang="jq">def is_prime:
. as $n
| if ($n < 2) then false
Line 789:
# for the sake of infinite-precision integer arithmetic
def power($b): . as $a | reduce range(0; $b) as $i (1; .*$a);
</syntaxhighlight>
</lang>
'''The task'''
<langsyntaxhighlight lang="jq"># Input: the limit
def wieferich:
primes[]
Line 798:
 
5000 | wieferich
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 806:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
println(filter(p -> (big"2"^(p - 1) - 1) % p^2 == 0, primes(5000))) # [1093, 3511]
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[WieferichPrimeQ]
WieferichPrimeQ[n_Integer] := PrimeQ[n] && Divisible[2^(n - 1) - 1, n^2]
Select[Range[5000], WieferichPrimeQ]</langsyntaxhighlight>
{{out}}
<pre>{1093, 3511}</pre>
Line 820:
=={{header|Nim}}==
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import math
import bignum
 
Line 839:
if p.isPrime:
if exp(two, p - 1, p * p) == 1: # Modular exponentiation.
echo p</langsyntaxhighlight>
 
{{out}}
Line 847:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">iswief(p)=if(isprime(p)&&(2^(p-1)-1)%p^2==0,1,0)
for(N=1,5000,if(iswief(N),print(N)))</langsyntaxhighlight>
{{out}}<pre>1093
3511</pre>
Line 854:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use feature 'say';
use ntheory qw(is_prime powmod);
 
say 'Wieferich primes less than 5000: ' . join ', ', grep { is_prime($_) and powmod(2, $_-1, $_*$_) == 1 } 1..5000;</langsyntaxhighlight>
{{out}}
<pre>Wieferich primes less than 5000: 1093, 3511</pre>
Line 863:
=={{header|Phix}}==
 
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 873:
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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;">"Weiferich primes less than 5000: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">weiferich</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
 
{{out}}
Line 880:
</pre>
alternative (same results), should be significantly faster, in the (largely pointless!) hunt for larger numbers.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 891:
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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;">"Weiferich primes less than 5000: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">weiferich</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de **Mod (X Y N)
(let M 1
(loop
Line 907:
(=1 (**Mod 2 (dec D) (* D D)))
(println D) )
(inc 'D (++ L)) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 915:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">#!/usr/bin/python
 
def isPrime(n):
Line 941:
for i in range(2, 5001):
if isWeiferich(i):
print(i)</langsyntaxhighlight>
{{out}}
<pre>Wieferich primes less than 5000:
Line 951:
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]].
 
<langsyntaxhighlight Quackerylang="quackery"> 5000 eratosthenes
[ dup isprime iff
Line 959:
else [ drop false ] ] is wieferich ( n --> b )
5000 times [ i^ wieferich if [ i^ echo cr ] ]</langsyntaxhighlight>
 
{{out}}
Line 970:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang typed/racket
(require math/number-theory)
 
Line 985:
p))
wieferich-primes<5000)
</syntaxhighlight>
</lang>
 
{{out}}
Line 991:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>put "Wieferich primes less than 5000: ", join ', ', ^5000 .grep: { .is-prime and not ( exp($_-1, 2) - 1 ) % .² };</langsyntaxhighlight>
{{out}}
<pre>Wieferich primes less than 5000: 1093, 3511</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays Wieferich primes which are under a specified limit N*/
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 5000 /*Not specified? Then use the default.*/
Line 1,029:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # Ps; assign next P; P sqare; P.*/
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,042:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
 
puts Prime.each(5000).select{|p| 2.pow(p-1 ,p*p) == 1 }
</syntaxhighlight>
</lang>
{{out}}
<pre>1093
Line 1,052:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
// mod_exp = "1.0"
Line 1,068:
println!("{}", p);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,078:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_wieferich_prime(p, base=2) {
powmod(base, p-1, p**2) == 1
}
 
say ("Wieferich primes less than 5000: ", 5000.primes.grep(is_wieferich_prime))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,090:
=={{header|Swift}}==
{{trans|C++}}
<langsyntaxhighlight lang="swift">func primeSieve(limit: Int) -> [Bool] {
guard limit > 0 else {
return []
Line 1,155:
for p in wieferichPrimes(limit: limit) {
print(p)
}</langsyntaxhighlight>
 
{{out}}
Line 1,167:
{{libheader|Wren-math}}
{{libheader|Wren-big}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/big" for BigInt
 
Line 1,176:
var den = p * p
if (num % den == 0) System.print(p)
}</langsyntaxhighlight>
 
{{out}}
10,333

edits