Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Ramanujan's_constant

View file

@ -0,0 +1,5 @@
Calculate Ramanujan's constant (as described on the [http://oeis.org/wiki/Ramanujan%27s_constant OEIS site]) with at least
32 digits of precision, by the method of your choice. Optionally, if using the 𝑒**(π*√''x'') approach,
show that when evaluated with the last four [https://en.wikipedia.org/wiki/Heegner_number Heegner numbers]
the result is ''almost'' an integer.

View file

@ -0,0 +1,26 @@
#include <iomanip>
#include <iostream>
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using big_float = boost::multiprecision::cpp_dec_float_100;
big_float f(unsigned int n) {
big_float pi(boost::math::constants::pi<big_float>());
return exp(sqrt(big_float(n)) * pi);
}
int main() {
std::cout << "Ramanujan's constant using formula f(N) = exp(pi*sqrt(N)):\n"
<< std::setprecision(80) << f(163) << '\n';
std::cout << "\nResult with last four Heegner numbers:\n";
std::cout << std::setprecision(30);
for (unsigned int n : {19, 43, 67, 163}) {
auto x = f(n);
auto c = ceil(x);
auto pc = 100.0 * (x/c);
std::cout << "f(" << n << ") = " << x << " = "
<< pc << "% of " << c << '\n';
}
return 0;
}

View file

@ -0,0 +1,40 @@
package main
import (
"fmt"
"github.com/ALTree/bigfloat"
"math/big"
)
const (
prec = 256 // say
ps = "3.1415926535897932384626433832795028841971693993751058209749445923078164"
)
func q(d int64) *big.Float {
pi, _ := new(big.Float).SetPrec(prec).SetString(ps)
t := new(big.Float).SetPrec(prec).SetInt64(d)
t.Sqrt(t)
t.Mul(pi, t)
return bigfloat.Exp(t)
}
func main() {
fmt.Println("Ramanujan's constant to 32 decimal places is:")
fmt.Printf("%.32f\n", q(163))
heegners := [4][2]int64{
{19, 96},
{43, 960},
{67, 5280},
{163, 640320},
}
fmt.Println("\nHeegner numbers yielding 'almost' integers:")
t := new(big.Float).SetPrec(prec)
for _, h := range heegners {
qh := q(h[0])
c := h[1]*h[1]*h[1] + 744
t.SetInt64(c)
t.Sub(t, qh)
fmt.Printf("%3d: %51.32f ≈ %18d (diff: %.32f)\n", h[0], qh, c, t)
}
}

View file

@ -0,0 +1,28 @@
import Control.Monad (forM_)
import Data.Number.CReal (CReal, showCReal)
import Text.Printf (printf)
ramfun :: CReal -> CReal
ramfun x = exp (pi * sqrt x)
-- Ramanujan's constant.
ramanujan :: CReal
ramanujan = ramfun 163
-- The last four Heegner numbers.
heegners :: [Int]
heegners = [19, 43, 67, 163]
-- The absolute distance to the nearest integer.
intDist :: CReal -> CReal
intDist x = abs (x - fromIntegral (round x))
main :: IO ()
main = do
let n = 35
printf "Ramanujan's constant: %s\n\n" (showCReal n ramanujan)
printf "%3s %34s%20s%s\n\n" " h " "e^(pi*sqrt(h))" "" " Dist. to integer"
forM_ heegners $ \h ->
let r = ramfun (fromIntegral h)
d = intDist r
in printf "%3d %54s %s\n" h (showCReal n r) (showCReal 15 d)

View file

@ -0,0 +1,8 @@
NB. takes the constant beyond the repeat 9s.
S=: cf_sqrt&163 Digits 34
P=: pi Digits 34
Y=: 1e_36 cf 1r8*P*S
f=: exp&Y M. NB. memoize
59j40 ": 8 ^~ f Digits 34
262537412640768743.9999999999992500711164316586918409066184
NB. ^

View file

@ -0,0 +1,87 @@
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Arrays;
import java.util.List;
public class RamanujanConstant {
public static void main(String[] args) {
System.out.printf("Ramanujan's Constant to 100 digits = %s%n%n", ramanujanConstant(163, 100));
System.out.printf("Heegner numbers yielding 'almost' integers:%n");
List<Integer> heegnerNumbers = Arrays.asList(19, 43, 67, 163);
List<Integer> heegnerVals = Arrays.asList(96, 960, 5280, 640320);
for ( int i = 0 ; i < heegnerNumbers.size() ; i++ ) {
int heegnerNumber = heegnerNumbers.get(i);
int heegnerVal = heegnerVals.get(i);
BigDecimal integer = BigDecimal.valueOf(heegnerVal).pow(3).add(BigDecimal.valueOf(744));
BigDecimal compute = ramanujanConstant(heegnerNumber, 50);
System.out.printf("%3d : %50s ~ %18s (diff ~ %s)%n", heegnerNumber, compute, integer, integer.subtract(compute, new MathContext(30)).toPlainString());
}
}
public static BigDecimal ramanujanConstant(int sqrt, int digits) {
// For accuracy on lat digit, computations with a few extra digits
MathContext mc = new MathContext(digits + 5);
return bigE(bigPi(mc).multiply(bigSquareRoot(BigDecimal.valueOf(sqrt), mc), mc), mc).round(new MathContext(digits));
}
// e = 1 + x/1! + x^2/2! + x^3/3! + ...
public static BigDecimal bigE(BigDecimal exponent, MathContext mc) {
BigDecimal e = BigDecimal.ONE;
BigDecimal ak = e;
int k = 0;
BigDecimal min = BigDecimal.ONE.divide(BigDecimal.TEN.pow(mc.getPrecision()));
while ( true ) {
k++;
ak = ak.multiply(exponent).divide(BigDecimal.valueOf(k), mc);
e = e.add(ak, mc);
if ( ak.compareTo(min) < 0 ) {
break;
}
}
return e;
}
// See : https://www.craig-wood.com/nick/articles/pi-chudnovsky/
public static BigDecimal bigPi(MathContext mc) {
int k = 0;
BigDecimal ak = BigDecimal.ONE;
BigDecimal a = ak;
BigDecimal b = BigDecimal.ZERO;
BigDecimal c = BigDecimal.valueOf(640320);
BigDecimal c3 = c.pow(3);
double digitePerTerm = Math.log10(c.pow(3).divide(BigDecimal.valueOf(24), mc).doubleValue()) - Math.log10(72);
double digits = 0;
while ( digits < mc.getPrecision() ) {
k++;
digits += digitePerTerm;
BigDecimal top = BigDecimal.valueOf(-24).multiply(BigDecimal.valueOf(6*k-5)).multiply(BigDecimal.valueOf(2*k-1)).multiply(BigDecimal.valueOf(6*k-1));
BigDecimal term = top.divide(BigDecimal.valueOf(k*k*k).multiply(c3), mc);
ak = ak.multiply(term, mc);
a = a.add(ak, mc);
b = b.add(BigDecimal.valueOf(k).multiply(ak, mc), mc);
}
BigDecimal total = BigDecimal.valueOf(13591409).multiply(a, mc).add(BigDecimal.valueOf(545140134).multiply(b, mc), mc);
return BigDecimal.valueOf(426880).multiply(bigSquareRoot(BigDecimal.valueOf(10005), mc), mc).divide(total, mc);
}
// See : https://en.wikipedia.org/wiki/Newton's_method#Square_root_of_a_number
public static BigDecimal bigSquareRoot(BigDecimal squareDecimal, MathContext mc) {
// Estimate
double sqrt = Math.sqrt(squareDecimal.doubleValue());
BigDecimal x0 = new BigDecimal(sqrt, mc);
BigDecimal two = BigDecimal.valueOf(2);
while ( true ) {
BigDecimal x1 = x0.subtract(x0.multiply(x0, mc).subtract(squareDecimal).divide(two.multiply(x0, mc), mc), mc);
String x1String = x1.toPlainString();
String x0String = x0.toPlainString();
if ( x1String.substring(0, x1String.length()-1).compareTo(x0String.substring(0, x0String.length()-1)) == 0 ) {
break;
}
x0 = x1;
}
return x0;
}
}

View file

@ -0,0 +1,5 @@
julia> a = BigFloat(MathConstants.e^(BigFloat(pi)))^(BigFloat(163.0)^0.5)
2.625374126407687439999999999992500725971981856888793538563373369908627075373427e+17
julia> 262537412640768744 - a
7.499274028018143111206461436626630091372924626572825942241598957614307213309258e-13

View file

@ -0,0 +1,7 @@
First[RealDigits[N[Exp[Pi Sqrt[163]], 200]]]
Table[
c = N[Exp[Pi Sqrt[h]], 40];
Log10[1 - FractionalPart[c]]
,
{h, {19, 43, 67, 163}}
]

View file

@ -0,0 +1,27 @@
import strformat, strutils
import decimal
setPrec(75)
let pi = newDecimal("3.1415926535897932384626433832795028841971693993751058209749445923078164")
proc eval(n: int): DecimalType =
result = exp(pi * sqrt(newDecimal(n)))
func format(n: DecimalType; d: Positive): string =
## Return the representation of "n" with "d" digits of precision.
let parts = ($n).split('.')
result = parts[0] & '.' & parts[1][0..<d]
echo "Ramanujans constant with 50 digits of precision:"
echo eval(163).format(50)
setPrec(50)
echo()
echo "Heegner numbers yielding 'almost' integers:"
for n in [19, 43, 67, 163]:
let x = eval(n)
let k = x.roundToInt
let d = x - k
let s = if d > 0: "+ " & $d else: "- " & $(-d)
echo &"{n:3}: {x}... = {k:>18} {s}..."

View file

@ -0,0 +1,2 @@
\p 50
exp(Pi*sqrt(163))

View file

@ -0,0 +1,27 @@
use strict;
use warnings;
use Math::AnyNum;
sub ramanujan_const {
my ($x, $decimals) = @_;
$x = Math::AnyNum->new($x);
my $prec = (Math::AnyNum->pi * $x->sqrt)/log(10) + $decimals + 1;
local $Math::AnyNum::PREC = 4*$prec->round->numify;
exp(Math::AnyNum->pi * $x->sqrt)->round(-$decimals)->stringify;
}
my $decimals = 100;
printf("Ramanujan's constant to $decimals decimals:\n%s\n\n",
ramanujan_const(163, $decimals));
print "Heegner numbers yielding 'almost' integers:\n";
my @tests = (19, 96, 43, 960, 67, 5280, 163, 640320);
while (@tests) {
my ($h, $x) = splice(@tests, 0, 2);
my $c = ramanujan_const($h, 32);
my $n = Math::AnyNum::ipow($x, 3) + 744;
printf("%3s: %51s ≈ %18s (diff: %s)\n", $h, $c, $n, ($n - $c)->round(-32));
}

View file

@ -0,0 +1,14 @@
use strict;
use Math::AnyNum <as_dec rat>;
sub continued_fr {
my ($a, $b, $n) = (@_[0,1], $_[2] // 100);
$a->() + ($n && $b->() / continued_fr($a, $b, $n-1));
}
my $r163 = continued_fr do {my $n; sub {$n++ ? 2*12 : 12 }}, do {my $n; sub { rat 19 }}, 40;
my $pi = continued_fr do {my $n; sub {$n++ ? 1 + 2*($n-2) : 0 }}, do {my $n; sub { rat($n++ ? ($n>2 ? ($n-1)**2 : 1) : 4)}}, 140;
my $p = $pi * $r163;
my $R = 1 + $p / continued_fr do { my $n; sub { $n++ ? $p+($n+0) : 1 } }, do {my $n; sub { $n++; -1*$n*$p }}, 180;
printf "Ramanujan's constant\n%s\n", as_dec($R,58);

View file

@ -0,0 +1,39 @@
(phixonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- no mpfr_exp() under p2js (yet), sorry</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (mpfr_set_default_prec[ision] has been renamed)</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #7060A8;">mpfr_set_default_precision</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">120</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (18 before, 100 after, plus 2 for kicks.)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">pi</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">mpfr_const_pi</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mpfr_exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">t</span>
<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;">"Ramanujan's constant to 100 decimal places is:\n"</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;">"%s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">(</span><span style="color: #000000;">163</span><span style="color: #0000FF;">),</span><span style="color: #000000;">100</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">heegners</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">96</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">43</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">960</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">67</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5280</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">163</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">640320</span><span style="color: #0000FF;">},</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;">"\nHeegner numbers yielding 'almost' integers:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(),</span> <span style="color: #000000;">qh</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">heegners</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">h0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h1</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">heegners</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">qh</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h0</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_add_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">744</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_set_z</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qh</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">qhs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qh</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">cs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32</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;">"%3d: %51s ~= %18s (diff: %s)\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">h0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">qhs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ts</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--

View file

@ -0,0 +1,8 @@
from mpmath import mp
heegner = [19,43,67,163]
mp.dps = 50
x = mp.exp(mp.pi*mp.sqrt(163))
print("calculated Ramanujan's constant: {}".format(x))
print("Heegner numbers yielding 'almost' integers:")
for i in heegner:
print(" for {}: {} ~ {} error: {}".format(str(i),mp.exp(mp.pi*mp.sqrt(i)),round(mp.exp(mp.pi*mp.sqrt(i))),(mp.pi*mp.sqrt(i)) - round(mp.pi*mp.sqrt(i))))

View file

@ -0,0 +1,35 @@
/*REXX pgm displays Ramanujan's constant to at least 100 decimal digits of precision. */
d= min( length(pi()), length(e()) ) - length(.) /*calculate max #decimal digs supported*/
parse arg digs sDigs . 1 . . $ /*obtain optional arguments from the CL*/
if digs=='' | digs=="," then digs= d /*Not specified? Then use the default.*/
if sDigs=='' | sDigs=="," then sDigs= d % 2 /* " " " " " " */
if $='' | $="," then $= 19 43 67 163 /* " " " " " " */
digs= min( digs, d) /*the minimum decimal digs for calc. */
sDigs= min(sDigs, d) /* " " " " display.*/
numeric digits digs /*inform REXX how many dec digs to use.*/
say "The value of Ramanujan's constant calculated with " d ' decimal digits of precision.'
say "shown with " sDigs ' decimal digits past the decimal point:'
say
do j=1 for words($); #= word($, j) /*process each of the Heegner numbers. */
say 'When using the Heegner number: ' # /*display which Heegner # is being used*/
z= exp(pi * sqrt(#) ) /*perform some heavy lifting here. */
say format(z, 25, sDigs); say /*display a limited amount of dec digs.*/
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
pi: pi= 3.1415926535897932384626433832795028841971693993751058209749445923078164062862,
|| 089986280348253421170679821480865132823066470938446095505822317253594081284,
|| 8111745028410270193852110555964462294895493038196; return pi
/*──────────────────────────────────────────────────────────────────────────────────────*/
e: e = 2.7182818284590452353602874713526624977572470936999595749669676277240766303535,
|| 475945713821785251664274274663919320030599218174135966290435729003342952605,
|| 9563073813232862794349076323382988075319525101901; return e
/*──────────────────────────────────────────────────────────────────────────────────────*/
exp: procedure; parse arg x; ix= x%1; if abs(x-ix)>.5 then ix= ix + sign(x); x= x-ix
z=1; _=1; w=z; do j=1; _= _*x/j; z=(z+_)/1; if z==w then leave; w=z; end
if z\==0 then z= z * e() ** ix; return z/1
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); h=d+6; numeric digits
numeric form; m.=9; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2
do j=0 while h>9; m.j=h; h=h % 2 + 1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g) * .5; end /*k*/; return g

View file

@ -0,0 +1,90 @@
use Rat::Precise;
# set the degree of precision for calculations
constant D = 54;
constant d = 15;
# two versions of exponentiation where base and exponent are both FatRat
multi infix:<**> (FatRat $base, FatRat $exp where * >= 1 --> FatRat) {
2 R** $base**($exp/2);
}
multi infix:<**> (FatRat $base, FatRat $exp where * < 1 --> FatRat) {
constant ε = 10**-D;
my $low = 0.FatRat;
my $high = 1.FatRat;
my $mid = $high / 2;
my $acc = my $sqr = sqrt($base);
while (abs($mid - $exp) > ε) {
$sqr = sqrt($sqr);
if ($mid <= $exp) { $low = $mid; $acc *= $sqr }
else { $high = $mid; $acc *= 1/$sqr }
$mid = ($low + $high) / 2;
}
$acc.substr(0, D).FatRat;
}
# calculation of π
sub π (--> FatRat) {
my ($a, $n) = 1, 1;
my $g = sqrt 1/2.FatRat;
my $z = .25;
my $pi;
for ^d {
given [ ($a + $g)/2, sqrt $a * $g ] {
$z -= (.[0] - $a)**2 * $n;
$n += $n;
($a, $g) = @$_;
$pi = ($a ** 2 / $z).substr: 0, 2 + D;
}
}
$pi.FatRat;
}
multi sqrt(FatRat $r --> FatRat) {
FatRat.new: sqrt($r.nude[0] * 10**(D*2) div $r.nude[1]), 10**D;
}
# integer roots
multi sqrt(Int $n) {
my $guess = 10**($n.chars div 2);
my $iterator = { ( $^x + $n div ($^x) ) div 2 };
my $endpoint = { $^x == $^y|$^z };
min ($guess, $iterator $endpoint)[*-1, *-2];
}
# 'cosmetic' cover to upgrade input to FatRat sqrt
sub prefix:<> (Int $n) { sqrt($n.FatRat) }
# calculation of 𝑒
sub postfix:<!> (Int $n) { (constant f = 1, |[\*] 1..*)[$n] }
sub 𝑒 (--> FatRat) { sum map { FatRat.new(1,.!) }, ^D }
# inputs, and their difference, formatted decimal-aligned
sub format ($a,$b) {
sub pad ($s) { ' ' x ((34 - d - 1) - ($s.split(/\./)[0]).chars) }
my $c = $b.precise(d, :z);
my $d = ($a-$b).precise(d, :z);
join "\n",
(sprintf "%11s {pad($a)}%s\n", 'Int', $a) ~
(sprintf "%11s {pad($c)}%s\n", 'Heegner', $c) ~
(sprintf "%11s {pad($d)}%s\n", 'Difference', $d)
}
# override built-in definitions
constant π = ();
constant 𝑒 = &𝑒();
my $Ramanujan = 𝑒**(π*163);
say "Ramanujan's constant to 32 decimal places:\nActual: " ~
"262537412640768743.99999999999925007259719818568888\n" ~
"Calculated: ", $Ramanujan.precise(32, :z), "\n";
say "Heegner numbers yielding 'almost' integers";
for 19, 96, 43, 960, 67, 5280, 163, 640320 -> $heegner, $x {
my $almost = 𝑒**(π*$heegner);
my $exact = $x³ + 744;
say format($exact, $almost);
}

View file

@ -0,0 +1,13 @@
use Rat::Precise;
sub continued-fraction($n, :@a, :@b) {
my $x = @a[0].FatRat;
$x = @a[$_ - 1] + @b[$_] / $x for reverse 1 ..^ $n;
$x;
}
#`{ √163 } my $r163 = continued-fraction( 50, :a(12,|((2*12) xx *)), :b(19 xx *));
#`{ π } my $pi = 4*continued-fraction(140, :a( 0,|(1, 3 ... *)), :b(4, 1, |((1, 2, 3 ... *) X** 2)));
#`{ e**x } my $R = 1 + ($_ / continued-fraction(170, :a( 1,|(2+$_, 3+$_ ... *)), :b(Nil, |(-1*$_, -2*$_ ... *) ))) given $r163*$pi;
say "Ramanujan's constant to 32 decimal places:\n", $R.precise(32);

View file

@ -0,0 +1,7 @@
require "bigdecimal/math"
include BigMath
e, pi = E(200), PI(200)
[19, 43, 67, 163].each do |x|
puts "#{x}: #{(e ** (pi * BigMath.sqrt(BigDecimal(x), 200))).round(100).to_s("F")}"
end

View file

@ -0,0 +1,15 @@
func ramanujan_const(x, decimals=32) {
local Num!PREC = *"#{4*round((Num.pi*√x)/log(10) + decimals + 1)}"
exp(Num.pi * √x) -> round(-decimals).to_s
}
var decimals = 100
printf("Ramanujan's constant to #{decimals} decimals:\n%s\n\n",
ramanujan_const(163, decimals))
say "Heegner numbers yielding 'almost' integers:"
[19, 96, 43, 960, 67, 5280, 163, 640320].each_slice(2, {|h,x|
var c = ramanujan_const(h, 32)
var n = (x**3 + 744)
printf("%3s: %51s ≈ %18s (diff: %s)\n", h, c, n, n-Num(c))
})

View file

@ -0,0 +1,39 @@
import "/big" for BigRat
import "/fmt" for Fmt
var pi = "3.1415926535897932384626433832795028841971693993751058209749445923078164"
var bigPi = BigRat.fromDecimal(pi)
var exp = Fn.new { |x, p|
var sum = x + 1
var prevTerm = x
var k = 2
var eps = BigRat.fromDecimal("0.5e-%(p)")
while (true) {
var nextTerm = prevTerm * x / k
sum = sum + nextTerm
if (nextTerm < eps) break
// speed up calculations by limiting precision to 'p' places
prevTerm = BigRat.fromDecimal(nextTerm.toDecimal(p))
k = k + 1
}
return sum
}
var ramanujan = Fn.new { |n, dp|
var e = bigPi * BigRat.new(n, 1).sqrt(70)
return exp.call(e, 70)
}
System.print("Ramanujan's constant to 32 decimal places is:")
System.print(ramanujan.call(163, 32).toDecimal(32))
var heegner = [19, 43, 67, 163]
System.print("\nHeegner numbers yielding almost integers:")
for (h in heegner) {
var r = ramanujan.call(h, 32)
var rc = r.ceil
var diff = (rc - r).toDecimal(32)
r = r.toDecimal(32)
rc = rc.toDecimal(32)
Fmt.print("$3d: $51s ≈ $18s (diff: $s)", h, r, rc, diff)
}