Pisano period: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 42:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F lcm(m, n)
R m I/ gcd(m, n) * n
 
Line 109:
print(‘pisano(n) for integers 'n' from 1 to 180 are:’)
L(n) 1..180
print(‘#3’.format(pisano(n)), end' I n % 15 == 0 {"\n"} E ‘ ’)</langsyntaxhighlight>
 
{{out}}
Line 179:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: formatting fry grouping io kernel math math.functions
math.primes math.primes.factors math.ranges sequences ;
 
Line 204:
"n pisano for integers 'n' from 2 to 180:" print
2 180 [a,b] [ pisano ] map 15 group
[ [ "%3d " printf ] each nl ] each</langsyntaxhighlight>
{{out}}
<pre style="height:45ex">
Line 272:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 407:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 476:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import qualified Data.Text as T
 
main = do
Line 630:
pisanoConjecture m = foldl1 lcm . map (uncurry pisanoPrime') $ factor m
where
pisanoPrime' p k = (p ^ (k - 1)) * pisanoPeriod p</langsyntaxhighlight>
{{out}}
<pre>PisanoPrime(p,2) for prime p lower than 15
Line 661:
Use efficient algorithm to calculate period.
 
<syntaxhighlight lang="java">
<lang Java>
import java.util.ArrayList;
import java.util.Collections;
Line 925:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,001:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
const pisanos = Dict{Int, Int}()
Line 1,035:
println("\nPisano(n) for n from 2 to 180:\n", [pisano(i) for i in 2:180])
println("\nPisano(n) using pisanoPrime for n from 2 to 180:\n", [pisanotask(i) for i in 2:180])
</langsyntaxhighlight>{{out}}
<pre>
pisanoPrime(2, 2) = 6
Line 1,094:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import math, strformat, tables
 
func primes(n: Positive): seq[int] =
Line 1,164:
echo "pisano(n) for integers 'n' from 1 to 180 are:"
for n in 1..180:
stdout.write &"{pisano(n):3}", if n mod 15 == 0: '\n' else: ' '</langsyntaxhighlight>
 
{{out}}
Line 1,233:
{{trans|Sidef}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,254:
say "Pisano periods for squares of primes p <= 50:\n", display( map { pisano_period_pp($_, 2) } @{primes(1, 50)} ),
"\nPisano periods for primes p <= 180:\n", display( map { pisano_period_pp($_, 1) } @{primes(1, 180)} ),
"\n\nPisano periods for integers n from 1 to 180:\n", display( map { pisano_period ($_ ) } 1..180 );</langsyntaxhighlight>
{{out}}
<pre>Pisano periods for squares of primes p <= 50:
Line 1,279:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pisano_period</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
Line 1,338:
<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;">"pisano(1..180):\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p180</span><span style="color: #0000FF;">,{</span><span style="color: #004600;">pp_IntFmt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">pp_IntCh</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,367:
Uses the [[wp:SymPy|SymPy]] library.
 
<langsyntaxhighlight lang="python">from sympy import isprime, lcm, factorint, primerange
from functools import reduce
 
Line 1,406:
print("\nPisano period (p) for integers 1 to 180")
for i in range(1, 181):
print(" %3d" % pisano2(i), end="" if i % 10 else "\n")</langsyntaxhighlight>
 
{{out}}
Line 1,439:
{{works with|Rakudo|2020.02}}
Didn't bother making two differently named routines, just made a multi that will auto dispatch to the correct candidate.
<syntaxhighlight lang="raku" perl6line>use Prime::Factor;
 
constant @fib := 1,1,*+*…*;
Line 1,463:
 
put "\nPisano period (p, 1) for integers 1 to 180";
.put for (1..180).map( { pisano-period($_) } )».fmt('%4d').batch(15);</langsyntaxhighlight>
{{out}}
<pre>Pisano period (p, 2) for primes less than 50
Line 1,488:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm calculates pisano period for a range of N, and pisanoPrime(N,m) [for primes]*/
numeric digits 500 /*ensure enough decimal digits for Fib.*/
parse arg lim.1 lim.2 lim.3 . /*obtain optional arguments from the CL*/
Line 1,524:
end /*k*/; @.m= k; return k
/*──────────────────────────────────────────────────────────────────────────────────────*/
pisanoPrime: procedure expose @. fib.; parse arg m,n; return m**(n-1) * pisano(m)</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
 
Line 1,592:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func pisano_period_pp(p,k) is cached {
 
assert(k.is_pos, "k = #{k} must be positive")
Line 1,616:
 
say "\nPisano periods for integers n from 1 to 180:"
say pisano_period.map(1..180)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,630:
 
By assuming that '''Wall-Sun-Sun primes''' do not exist, we can compute the Pisano period more efficiently, as illustrated below on Fermat numbers '''F_n = 2^(2^n) + 1''':
<langsyntaxhighlight lang="ruby">func pisano_period_pp(p, k=1) {
(p - kronecker(5, p)).divisors.first_by {|d| fibmod(d, p) == 0 } * p**(k-1)
}
Line 1,651:
for k in (1..8) {
say ("Pisano(F_#{k}) = ", pisano_period(2**(2**k) + 1))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,668:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
 
Line 1,725:
if (n != 1 && n%15 == 0) System.print()
}
System.print()</langsyntaxhighlight>
 
{{out}}
Line 1,795:
=={{header|zkl}}==
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library for prime testing
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
 
fcn pisanoPeriod(p){
Line 1,809:
_assert_(BI(p).probablyPrime(), "%s is not a prime number".fmt(p));
pisanoPeriod(p.pow(k))
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("Pisano period (p, 2) for primes less than 50:");
[1..50].pump(List,BI,"probablyPrime",Void.Filter, pisanoPrime.fp1(2))
.concat(" "," ").println();
Line 1,816:
println("Pisano period (p, 1) for primes less than 180:");
[1..180].pump(List,BI,"probablyPrime",Void.Filter, pisanoPrime.fp1(1))
.pump(Void,T(Void.Read,14,False),fcn{ vm.arglist.apply("%4d".fmt).concat().println() });</langsyntaxhighlight>
{{out}}
<pre>
Line 1,826:
256 130 276 46 148 50 316 328 336 348 178
</pre>
<langsyntaxhighlight lang="zkl">fcn pisano(m){
primeFactors(m).pump(Dictionary().incV) //18 --> (2,3,3) --> ("2":1, "3":2)
.reduce(fcn(z,[(k,v])){ lcm(z,pisanoPrime(k.toInt(),v)) },1)
Line 1,844:
if(n!=m) acc.append(n/m); // opps, missed last factor
else acc;
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("Pisano(m) for integers 1 to 180:");
[1..180].pump(List, pisano, "%4d".fmt)
.pump(Void,T(Void.Read,14,False),fcn{ vm.arglist.concat().println() });</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits