P-Adic numbers, basic: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Now uses new core library method.)
m (syntax highlighting fixup automation)
Line 52: Line 52:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
' ***********************************************
' ***********************************************
'subject: convert two rationals to p-adic numbers,
'subject: convert two rationals to p-adic numbers,
Line 413: Line 413:


system
system
</syntaxhighlight>
</lang>
{{out|Examples}}
{{out|Examples}}
<pre>
<pre>
Line 600: Line 600:
=={{header|Go}}==
=={{header|Go}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 929: Line 929:
fmt.Println()
fmt.Println()
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,101: Line 1,101:
p-Adic arithmetics and conversion between rationals is implemented as instances of <code>Eq</code>, <code>Num</code>, <code>Fractional</code> and <code>Real</code> classes, so, they could be treated as usual real numbers (up to existence of some rationals for non-prime bases).
p-Adic arithmetics and conversion between rationals is implemented as instances of <code>Eq</code>, <code>Num</code>, <code>Fractional</code> and <code>Real</code> classes, so, they could be treated as usual real numbers (up to existence of some rationals for non-prime bases).


<lang haskell>{-# LANGUAGE KindSignatures, DataKinds #-}
<syntaxhighlight lang="haskell">{-# LANGUAGE KindSignatures, DataKinds #-}
module Padic where
module Padic where


Line 1,316: Line 1,316:
let m = (x*r) `mod` p
let m = (x*r) `mod` p
_:zs = subMod p (x:xs) (mulMod p [m] (b:bs))
_:zs = subMod p (x:xs) (mulMod p [m] (b:bs))
in m : go zs</lang>
in m : go zs</syntaxhighlight>


Convertation between rationals and p-adic numbers
Convertation between rationals and p-adic numbers
Line 1,364: Line 1,364:
Uses the Nemo abstract algebra library. The Nemo library's rational reconstruction function gives up quite easily,
Uses the Nemo abstract algebra library. The Nemo library's rational reconstruction function gives up quite easily,
so another alternative to FreeBasic's crat() using vector products is below.
so another alternative to FreeBasic's crat() using vector products is below.
<lang Julia>using Nemo, LinearAlgebra
<syntaxhighlight lang="julia">using Nemo, LinearAlgebra


set_printing_mode(FlintPadicField, :terse)
set_printing_mode(FlintPadicField, :terse)
Line 1,426: Line 1,426:
println(a, "\n", dstring(a), "\n", b, "\n", dstring(b), "\n+ =\n", c, "\n", dstring(c), " $r\n")
println(a, "\n", dstring(a), "\n", b, "\n", dstring(b), "\n+ =\n", c, "\n", dstring(c), " $r\n")
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
2 + O(2^5)
2 + O(2^5)
Line 1,576: Line 1,576:
{{trans|Go}}
{{trans|Go}}
Translation of Go with some modifications, especially using exceptions when an error is encountered.
Translation of Go with some modifications, especially using exceptions when an error is encountered.
<lang Nim>import math, strformat
<syntaxhighlight lang="nim">import math, strformat


const
const
Line 1,801: Line 1,801:
echo ""
echo ""
except PadicError:
except PadicError:
echo getCurrentExceptionMsg()</lang>
echo getCurrentExceptionMsg()</syntaxhighlight>


{{out}}
{{out}}
Line 1,967: Line 1,967:
=={{header|Phix}}==
=={{header|Phix}}==
{{libheader|Phix/Class}}
{{libheader|Phix/Class}}
<lang Phix>// constants
<syntaxhighlight lang="phix">// constants
constant EMX = 64 // exponent maximum (if indexing starts at -EMX)
constant EMX = 64 // exponent maximum (if indexing starts at -EMX)
constant DMX = 1e5 // approximation loop maximum
constant DMX = 1e5 // approximation loop maximum
Line 2,234: Line 2,234:
end if
end if
printf(1,"\n")
printf(1,"\n")
end for</lang>
end for</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,259: Line 2,259:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6># 20210225 Raku programming solution
<syntaxhighlight lang="raku" line># 20210225 Raku programming solution


#!/usr/bin/env raku
#!/usr/bin/env raku
Line 2,353: Line 2,353:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,364: Line 2,364:
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-dynamic}}
<lang ecmascript>import "/dynamic" for Struct
<syntaxhighlight lang="ecmascript">import "/dynamic" for Struct


// constants
// constants
Line 2,637: Line 2,637:
}
}
System.print()
System.print()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}