September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,45 @@
// version 1.1.3
import java.math.BigInteger
class Fraction(val num: BigInteger, val denom: BigInteger) {
operator fun times(n: BigInteger) = Fraction (n * num, denom)
fun isIntegral() = num % denom == BigInteger.ZERO
}
fun String.toFraction(): Fraction {
val split = this.split('/')
return Fraction(BigInteger(split[0]), BigInteger(split[1]))
}
val BigInteger.isPowerOfTwo get() = this.and(this - BigInteger.ONE) == BigInteger.ZERO
val log2 = Math.log(2.0)
fun fractran(program: String, n: Int, limit: Int, primesOnly: Boolean): List<Int> {
val fractions = program.split(' ').map { it.toFraction() }
val results = mutableListOf<Int>()
if (!primesOnly) results.add(n)
var nn = BigInteger.valueOf(n.toLong())
while (results.size < limit) {
val frac = fractions.find { (it * nn).isIntegral() } ?: break
nn = nn * frac.num / frac.denom
if (!primesOnly) {
results.add(nn.toInt())
}
else if (primesOnly && nn.isPowerOfTwo) {
val prime = (Math.log(nn.toDouble()) / log2).toInt()
results.add(prime)
}
}
return results
}
fun main(args: Array<String>) {
val program = "17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/14 15/2 55/1"
println("First twenty numbers:")
println(fractran(program, 2, 20, false))
println("\nFirst twenty primes:")
println(fractran(program, 2, 20, true))
}

View file

@ -1,5 +1,5 @@
sub fractran(@program) {
2, { +first Int, map (* * $_).narrow, @program } ... 0
2, { first Int, map (* * $_).narrow, @program } ... 0
}
say fractran(<17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11
15/14 15/2 55/1>)[^100];

View file

@ -1,4 +1,4 @@
for fractran <17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11
15/14 15/2 55/1> {
say $++, "\t", .msb, "\t", $_ if .log %% log(2);
say $++, "\t", .msb, "\t", $_ if 1 +< .msb == $_;
}

View file

@ -0,0 +1,62 @@
(import (scheme base)
(scheme inexact)
(scheme read)
(scheme write))
(define *string-fractions* ; string input of fractions
"17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19
1/17 11/13 13/11 15/14 15/2 55/1")
(define *fractions* ; create vector of fractions from string input
(list->vector ; convert result to a vector, for constant access times
(read (open-input-string ; read from the string of fractions, as a list
(string-append "(" *string-fractions* ")")))))
;; run a fractran interpreter, returning the next number for n
;; or #f if no next number available
;; assume fractions: ordered vector of positive fractions
;; n: a positive integer
(define (fractran fractions n)
(let ((max-n (vector-length fractions)))
(let loop ((i 0))
(cond ((= i max-n)
#f)
((integer? (* n (vector-ref fractions i)))
(* n (vector-ref fractions i)))
(else
(loop (+ 1 i)))))))
;; Task
(define (display-result max-n)
(do ((i 0 (+ 1 i))
(n 2 (fractran *fractions* n)))
((= i max-n) (newline))
(display n) (display " ")))
(display "Task: ")
(display-result 20) ; show first 20 numbers
;; Extra Credit: derive first 20 prime numbers
(define (generate-primes target-number initial-n)
(define (is-power-of-two? n)
(and (> n 2)
(integer? (log n 2))))
(define (extract-prime n)
(exact (log n 2)))
;
(let loop ((count 0)
(n initial-n))
(when (< count target-number)
(cond ((eq? n #f)
(display "-- FAILED TO COMPUTE N --\n"))
((is-power-of-two? n)
(display (extract-prime n)) (display " ")
(loop (+ 1 count)
(fractran *fractions* n)))
(else
(loop count
(fractran *fractions* n))))))
(newline))
(display "Primes:\n")
(generate-primes 20 2) ; create first 20 primes

View file

@ -1,20 +1,20 @@
var str ="17/91, 78/85, 19/51, 23/38, 29/33, 77/29, 95/23, 77/19, 1/17, 11/13, 13/11, 15/14, 15/2, 55/1"
const FractalProgram = str.split(',').map{.to_r} #=> array of rationals
const FractalProgram = str.split(',').map{.num} #=> array of rationals
func runner(n, callback) {
var num = 2.rat
var num = 2
n.times {
callback(num *= FractalProgram.find { |f| (f*num).de.is_one })
callback(num *= FractalProgram.find { |f| f * num -> is_int })
}
}
func prime_generator(n, callback) {
var x = 0;
runner(Math.inf, { |num|
var l = num.to_f.log2
runner(Inf, { |num|
var l = num.log2
if (l.floor == l) {
callback(l.to_i)
++x == n && return
callback(l.int)
++x == n && return nil
}
})
}

View file

@ -0,0 +1,15 @@
var fracs="17/91, 78/85, 19/51, 23/38, 29/33, 77/29, 95/23, 77/19, 1/17,"
"11/13, 13/11, 15/14, 15/2, 55/1";
fcn fractranW(n,fracsAsOneBigString){ //-->Walker (iterator)
fracs:=(fracsAsOneBigString-" ").split(",").apply(
fcn(frac){ frac.split("/").apply("toInt") }); //( (n,d), (n,d), ...)
Walker(fcn(rn,fracs){
n:=rn.value;
foreach a,b in (fracs){
if(n*a%b == 0){
rn.set(n*a/b);
return(n);
}
}
}.fp(Ref(n),fracs))
}

View file

@ -0,0 +1 @@
fractranW(2,fracs).walk(20).println();

View file

@ -0,0 +1,11 @@
var [const] BN=Import("zklBigNum"); // libGMP
fcn fractranPrimes{
foreach n,fr in ([1..].zip(fractranW(BN(2),fracs))){
if(fr.num1s==1){
p:=(fr.toString(2) - "1").len(); // count zeros
if(p>1)
println("Prime %3d from the nth Fractran(%8d): %d".fmt(p,n,fr));
}
}
}
fractranPrimes();