Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
37
Task/Sylvesters-sequence/Agena/sylvesters-sequence.agena
Normal file
37
Task/Sylvesters-sequence/Agena/sylvesters-sequence.agena
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
scope # calculate elements of Sylvester's Sequence
|
||||
|
||||
import mapm; # explicit import needed for: Linux, Mac OS X, Windows and Solaris
|
||||
|
||||
mapm.xdigits( 220 );
|
||||
|
||||
# returns a sequence set to the first n elements of Sylvester's Sequence
|
||||
# starting from 2, the elements are the product of the previous elements plus 1
|
||||
local constant b0, constant b1, constant b2 := mapm.xnumber( 0 ), mapm.xnumber( 1 ), mapm.xnumber( 2 );
|
||||
local proc sylvester( n :: integer ) :: sequence
|
||||
local result; create sequence result( n );
|
||||
local product := b2;
|
||||
result[ 1 ] := product;
|
||||
for i from 2 to n do
|
||||
result[ i ] := product + b1;
|
||||
product *:= result[ i ]
|
||||
od;
|
||||
return result
|
||||
end;
|
||||
|
||||
# returns a string representation of the integer portion of an xnumber
|
||||
local constant asstring := proc( n :: xnumber ) :: string
|
||||
local constant str := mapm.xtostring( n );
|
||||
local constant point := "." in str;
|
||||
return if point <> null then str[ 1 to point - 1 ] else str fi;
|
||||
end;
|
||||
|
||||
scope # show the sequence and sum the reciprocals
|
||||
local constant sseq := sylvester( 10 );
|
||||
local reciprocalSum := b0;
|
||||
for i from 1 to size sseq do
|
||||
print( asstring( sseq[ i ] ) );
|
||||
reciprocalSum +:= b1 / sseq[ i ]
|
||||
od;
|
||||
printf( "Sum of reciprocals: %s\n", mapm.xtostring( reciprocalSum ) )
|
||||
epocs
|
||||
epocs
|
||||
18
Task/Sylvesters-sequence/Crystal/sylvesters-sequence.cr
Normal file
18
Task/Sylvesters-sequence/Crystal/sylvesters-sequence.cr
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
require "big"
|
||||
|
||||
def sylvester
|
||||
accum = 1.to_big_i
|
||||
curr = 1.to_big_i
|
||||
Iterator.of {
|
||||
accum *= curr
|
||||
curr = accum + 1
|
||||
}
|
||||
end
|
||||
|
||||
puts "First 10 elements:"
|
||||
sylvester.first(10).each do |s|
|
||||
puts s
|
||||
end
|
||||
|
||||
print "\nSum of the reciprocals of the first 10 elements: "
|
||||
puts sylvester.first(10).sum {|s| BigRational.new(1, s) }
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
local fn SylvesterSequence( terms as int )
|
||||
NSLog( @"First %d terms of the Sylvester's sequence:", terms )
|
||||
double sum = 0
|
||||
long sylv
|
||||
for int i = 1 to terms
|
||||
if ( i == 1 ) then sylv = 2 else sylv = sylv * sylv - sylv + 1
|
||||
NSLog( @"%2d : %llu", i, sylv )
|
||||
sum = sum + 1 / (double)sylv
|
||||
next
|
||||
NSLog( @"\nSum of the reciprocals: %.15f", sum )
|
||||
end fn
|
||||
|
||||
fn SylvesterSequence( 7 )
|
||||
|
||||
HandleEvents
|
||||
78
Task/Sylvesters-sequence/JavaScript/sylvesters-sequence.js
Normal file
78
Task/Sylvesters-sequence/JavaScript/sylvesters-sequence.js
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
class BigRational {
|
||||
constructor(numerator, denominator) {
|
||||
this.numerator = BigInt(numerator);
|
||||
this.denominator = BigInt(denominator);
|
||||
|
||||
const gcd = this.gcd(this.numerator, this.denominator);
|
||||
this.numerator = this.numerator / gcd;
|
||||
this.denominator = this.denominator / gcd;
|
||||
}
|
||||
|
||||
gcd(a, b) {
|
||||
a = a < 0n ? -a : a;
|
||||
b = b < 0n ? -b : b;
|
||||
while (b !== 0n) {
|
||||
const temp = b;
|
||||
b = a % b;
|
||||
a = temp;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
add(other) {
|
||||
const numer = this.numerator * other.denominator + this.denominator * other.numerator;
|
||||
const denom = this.denominator * other.denominator;
|
||||
return new BigRational(numer, denom);
|
||||
}
|
||||
|
||||
toDecimal(decimalPlaces) {
|
||||
// Scale up the numerator to get the desired precision
|
||||
const scale = BigInt(10) ** BigInt(decimalPlaces + 10);
|
||||
const scaledNumerator = this.numerator * scale;
|
||||
const quotient = scaledNumerator / this.denominator;
|
||||
|
||||
// Convert to string and insert decimal point
|
||||
let str = quotient.toString();
|
||||
const totalDigits = str.length;
|
||||
const integerDigits = totalDigits - decimalPlaces - 10;
|
||||
|
||||
if (integerDigits <= 0) {
|
||||
str = '0.' + '0'.repeat(-integerDigits) + str;
|
||||
} else {
|
||||
str = str.slice(0, integerDigits) + '.' + str.slice(integerDigits);
|
||||
}
|
||||
|
||||
// Round and truncate to the desired decimal places
|
||||
const dotIndex = str.indexOf('.');
|
||||
return str.slice(0, dotIndex + decimalPlaces + 1);
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `${this.numerator} / ${this.denominator}`;
|
||||
}
|
||||
|
||||
static get ZERO() {
|
||||
return new BigRational(0n, 1n);
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
console.log("The first 10 terms in Sylvester's sequence are:");
|
||||
let term = 2n;
|
||||
let sum = BigRational.ZERO;
|
||||
|
||||
for (let i = 1; i <= 10; i++) {
|
||||
console.log(term.toString());
|
||||
sum = sum.add(new BigRational(1n, term));
|
||||
term = term * term - term + 1n;
|
||||
}
|
||||
console.log();
|
||||
|
||||
console.log("The sum of their reciprocals as a rational number is:");
|
||||
console.log(sum.toString() + '\n');
|
||||
|
||||
console.log("The sum of their reciprocals as a decimal number, to 235 decimal places, is:");
|
||||
console.log(sum.toDecimal(235));
|
||||
}
|
||||
|
||||
main();
|
||||
26
Task/Sylvesters-sequence/Pluto/sylvesters-sequence.pluto
Normal file
26
Task/Sylvesters-sequence/Pluto/sylvesters-sequence.pluto
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
require "bignum"
|
||||
|
||||
local sylvester = {bigint.two}
|
||||
mpz.init() -- use GMP
|
||||
mpq.init()
|
||||
local a = mpz.set(1, 2)
|
||||
local count = 1
|
||||
while true do
|
||||
local next = mpz.get(a) + 1
|
||||
sylvester:insert(next)
|
||||
count += 1
|
||||
if count == 10 then break end
|
||||
mpz.mul(a, next)
|
||||
end
|
||||
mpz.clear(a)
|
||||
print("The first 10 terms in the Sylvester sequence are:")
|
||||
print(sylvester:mapped(|e| -> e:tostring()):concat("\n"))
|
||||
|
||||
a = mpq.set(a, 0)
|
||||
for sylvester as s do mpq.add(a, bigrat.recip(s)) end
|
||||
local sum_recip = mpq.get(a)
|
||||
mpq.clear(a)
|
||||
print("\nThe sum of their reciprocals as a rational number is:")
|
||||
print(sum_recip)
|
||||
print("\nThe sum of their reciprocals as a decimal number (to 211 places) is:")
|
||||
print(sum_recip:todecimal(211))
|
||||
10
Task/Sylvesters-sequence/R/sylvesters-sequence.r
Normal file
10
Task/Sylvesters-sequence/R/sylvesters-sequence.r
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
library(gmp)
|
||||
|
||||
sylvester <- function(n){
|
||||
if(n==1) as.bigz(2)
|
||||
else 1+Reduce(`*`, lapply(seq_len(n-1), sylvester))
|
||||
}
|
||||
|
||||
vals <- print(lapply(1:10, sylvester), initLine=FALSE)
|
||||
recips <- lapply(vals, function(x) 1/x)
|
||||
print(Reduce(`+`, recips), initLine=FALSE)
|
||||
2
Task/Sylvesters-sequence/Uiua/sylvesters-sequence.uiua
Normal file
2
Task/Sylvesters-sequence/Uiua/sylvesters-sequence.uiua
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
~ "git:github.com/mykdavies/uiua-bigint" ~ Add Big BigS Mul
|
||||
≡(&p□°BigS)⍥(⬚0˜⊂Add1⊸/Mul)9[Big 2]
|
||||
Loading…
Add table
Add a link
Reference in a new issue