Padovan sequence: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 70:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">V pp = 1.324717957244746025960908854
V ss = 1.0453567932525329623
V Rules = [‘A’ = ‘B’, ‘B’ = ‘C’, ‘C’ = ‘AB’]
Line 121:
V list3 = padovan3(32).map(x -> x.len)
print(list3.join(‘ ’))
print(‘These lengths are’(I list3 == list1[0.<32] {‘ ’} E ‘ not ’)‘the 32 first terms of the Padovan sequence.’)</langsyntaxhighlight>
 
{{out}}
Line 138:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # show members of the Padovan Sequence calculated in various ways #
# returns the first n elements of the Padovan sequence by the #
# recurance relation: P(n)=P(n-2)+P(n-3) #
Line 222:
)
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 235:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">--------------------- PADOVAN NUMBERS --------------------
 
-- padovans :: [Int]
Line 502:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre>First 20 padovans:
Line 521:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Line 618:
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 640:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <map>
#include <cmath>
Line 721:
"floor- and L-system-based", 32);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 743:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(def padovan (map first (iterate (fn [[a b c]] [b c (+ a b)]) [1 1 1])))
 
(def pad-floor
Line 782:
"The L-system, recurrence and floor based algorithms "
(if (comp-all 32) "match" "not match")
" to n=32"))))</langsyntaxhighlight>
 
{{out}}
Line 802:
Thanks Rudy Velthuis for the [https://github.com/rvelthuis/DelphiBigNumbers Velthuis.BigDecimals] library.<br>
Boost.Generics.Collection is part of [https://github.com/MaiconSoft/DelphiBoostLib DelphiBoostLib].
<syntaxhighlight lang="delphi">
<lang Delphi>
program Padovan_sequence;
 
Line 905:
end, 'floor- and L-system-based', 32);
readln;
end.</langsyntaxhighlight>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: L-system accessors io kernel make math math.functions
memoize prettyprint qw sequences ;
 
Line 940:
 
32 <iota> [ pfloor ] map 32 plsys [ length ] map assert=
"The L-system, recurrence and floor based algorithms match to n=31." print</langsyntaxhighlight>
{{out}}
<pre>
Line 967:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,073:
}
fmt.Println("\nThe recurrence and L-system based functions", s, "the same results for 32 terms.")
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,092:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">-- list of Padovan numbers using recurrence
pRec = map (\(a,_,_) -> a) $ iterate (\(a,b,c) -> (b,c,a+b)) (1,1,1)
 
Line 1,132:
putStr $ if checkN 32 pFloor (map length lSystem)
then "match" else "do not match"
putStr " from P_0 to P_31.\n"</langsyntaxhighlight>
 
{{out}}
Line 1,147:
 
and a variant expressed in terms of '''unfoldr''', which allows for a coherent treatment of these three cases – isolating the respects in which they differ – and also lends itself to a simple translation to the N-Step Padovan case, covered in another task.
<langsyntaxhighlight lang="haskell">import Data.List (unfoldr)
 
--------------------- PADOVAN NUMBERS --------------------
Line 1,203:
 
prefixesMatch :: Eq a => [a] -> [a] -> Int -> Bool
prefixesMatch xs ys n = and (zipWith (==) (take n xs) ys)</langsyntaxhighlight>
{{Out}}
<pre>First 20 padovans:
Line 1,226:
 
Implementation:
<syntaxhighlight lang="j">
<lang J>
padovanSeq=: (],+/@(_2 _3{]))^:([-3:)&1 1 1
 
Line 1,234:
padovanL=: rplc&('A';'B'; 'B';'C'; 'C';'AB')@]^:[&'A'
seqLen=. #@(-.&' ')"1
</syntaxhighlight>
</lang>
 
Typically, inductive sequences based on a function F with an initial value G can be expressed using an expression of the form F@]^:[@G or something similar. Here, [ represents the argument to the derived recurrence function. For padovanSeq, we are generating a sequence, so we want to retain the previous values. So instead of just using f=: +/@(_2 3{]) which adds up the second and third numbers from the end of the sequence, we also retain the previous values using F=: ],f
Line 1,242:
Task examples:
 
<syntaxhighlight lang="j">
<lang J>
padovanSeq 20
1 1 1 2 2 3 4 5 7 9 12 16 21 28 37 49 65 86 114 151
Line 1,260:
(padovanSeq 32) -: seqLen padovanL i.32
1
</syntaxhighlight>
</lang>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 1,457:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>First 20 padovans:
Line 1,483:
streams of the Padovan strings and numbers respectively.
 
<langsyntaxhighlight lang="jq"># Output: first $n Padovans
def padovanRecur($n):
[range(0;$n) | 1] as $p
Line 1,531:
;
 
task</langsyntaxhighlight>
{{out}}
<pre>
Line 1,548:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">""" Recursive Padovan """
rPadovan(n) = (n < 4) ? one(n) : rPadovan(n - 3) + rPadovan(n - 2)
 
Line 1,574:
foreach(i -> println(rpad(i, 4), rpad(lr[i], 12), rpad(lf[i], 12),
rpad(i < 33 ? lL[i] : "", 12), (i < 11 ? sL[i] : "")), 1:64)
</langsyntaxhighlight>{{out}}
<pre>
N Recursive Floor LSystem String
Line 1,645:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Padovan1,a,p,s]
p=N[Surd[((9+Sqrt[69])/18),3]+Surd[((9-Sqrt[69])/18),3],200];
s=1.0453567932525329623;
Line 1,654:
Padovan1[64]===Padovan2[64]
SubstitutionSystem[{"A"->"B","B"->"C","C"->"AB"},"A",10]//Column
(StringLength/@SubstitutionSystem[{"A"->"B","B"->"C","C"->"AB"},"A",31])==Padovan2[32]</langsyntaxhighlight>
{{out}}
<pre>{1,1,1,2,2,3,4,5,7,9,12,16,21,28,37,49,65,86,114,151}
Line 1,673:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import sequtils, strutils, tables
 
const
Line 1,734:
echo "These lengths are",
if list3 == list1[0..31]: " " else: " not ",
"the 32 first terms of the Padovan sequence."</langsyntaxhighlight>
 
{{out}}
Line 1,750:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <state say>;
Line 1,774:
 
$pr[$_] == $pf[$_] and $pr[$_] == length $L[$_] or die "Uh oh, n=$_: $pr[$_] vs $pf[$_] vs " . length $L[$_] for 0 .. $n-1;
say '100% agreement among all 3 methods.';</langsyntaxhighlight>
{{out}}
<pre>1 1 1 2 2 3 4 5 7 9 12 16 21 28 37 49 65 86 114 151
Line 1,783:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">padovan</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
Line 1,818:
<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;">"The first 10 L-system strings: %v\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">l10</span><span style="color: #0000FF;">})</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;">"recursive, algorithmic, and l-system agree to n=64\n"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,830:
=={{Header|Python}}==
===Python: Idiomatic===
<langsyntaxhighlight lang="python">from math import floor
from collections import deque
from typing import Dict, Generator
Line 1,878:
print("\nThe L-system, recurrence and floor based algorithms match to n=31 .")
else:
print("\nThe L-system, recurrence and floor based algorithms DIFFER!")</langsyntaxhighlight>
 
{{out}}
Line 1,901:
 
===Python: Expressed in terms of a generic anamorphism (unfoldr)===
<langsyntaxhighlight lang="python">'''Padovan series'''
 
from itertools import chain, islice
Line 2,036:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First 20 padovans:
Line 2,059:
<code>v**</code> is defined at [[Exponentiation operator#Quackery]].
 
<langsyntaxhighlight Quackerylang="quackery">( --------------------- Recurrence -------------------- )
[ dup 0 = iff
Line 2,125:
[ say "The first 32 recurrence terms and L System lengths are the same." ]
else [ say "Oh no! It's all gone pear-shaped!" ]
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,139:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>constant p = 1.32471795724474602596;
constant s = 1.0453567932525329623;
constant %rules = A => 'B', B => 'C', C => 'AB';
Line 2,154:
 
say "Recurrence == Floor to N=64" if (@pad-recur Z== @pad-floor).head(64).all;
say "Recurrence == L-len to N=32" if (@pad-recur Z== @pad-L-len).head(32).all;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,164:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm computes the Padovan seq. (using 2 methods), and also computes the L─strings.*/
numeric digits 40 /*better precision for Plastic ratio. */
parse arg n nF Ln cL . /*obtain optional arguments from the CL*/
Line 2,214:
end /*j*/
say
if ok then say 'all ' cL what; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,228:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">padovan = Enumerator.new do |y|
ar = [1, 1, 1]
loop do
Line 2,259:
bool = l_system.take(n).map(&:size) == padovan.take(n)
puts "Sizes of first #{n} l_system strings equal to recurrence padovan? #{bool}."
</syntaxhighlight>
</lang>
{{out}}
<pre>Recurrence Padovan: [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151]
Line 2,269:
</pre>
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn padovan_recur() -> impl std::iter::Iterator<Item = usize> {
let mut p = vec![1, 1, 1];
let mut n = 0;
Line 2,330:
.eq(padovan_recur().take(32))
);
}</langsyntaxhighlight>
 
{{out}}
Line 2,346:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
class PadovanRecurrence: Sequence, IteratorProtocol {
Line 2,410:
b = PadovanLSystem().prefix(32).map{$0.count}
.elementsEqual(PadovanRecurrence().prefix(32))
print("\nLength of first 32 strings produced from the L-system = Padovan sequence? \(b)")</langsyntaxhighlight>
 
{{out}}
Line 2,429:
{{libheader|Wren-dynamic}}
L-System stuff is based on the Julia implementation.
<langsyntaxhighlight lang="ecmascript">import "/big" for BigRat
import "/dynamic" for Struct
 
Line 2,492:
areSame = (0...32).all { |i| recur[i] == lsyst[i] }
s = areSame ? "give" : "do not give"
System.print("\nThe recurrence and L-system based functions %(s) the same results for 32 terms.")</langsyntaxhighlight>
 
{{out}}
10,327

edits