September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,56 @@
|
|||
(defun stern-brocot (numbers)
|
||||
(declare ((or null (vector integer)) numbers))
|
||||
(cond ((null numbers)
|
||||
(setf numbers (make-array 2 :element-type 'integer :adjustable t :fill-pointer t
|
||||
:initial-element 1)))
|
||||
((zerop (length numbers))
|
||||
(vector-push-extend 1 numbers)
|
||||
(vector-push-extend 1 numbers))
|
||||
(t
|
||||
(assert (evenp (length numbers)))
|
||||
(let* ((considered-index (/ (length numbers) 2))
|
||||
(considered (aref numbers considered-index))
|
||||
(precedent (aref numbers (1- considered-index))))
|
||||
(vector-push-extend (+ considered precedent) numbers)
|
||||
(vector-push-extend considered numbers))))
|
||||
numbers)
|
||||
|
||||
(defun first-15 ()
|
||||
(loop for input = nil then seq
|
||||
for seq = (stern-brocot input)
|
||||
while (< (length seq) 15)
|
||||
finally (format t "First 15: ~{~A~^ ~}~%" (coerce (subseq seq 0 15) 'list))))
|
||||
|
||||
(defun first-1-to-10 ()
|
||||
(loop with seq = (stern-brocot nil)
|
||||
for i from 1 to 10
|
||||
for index = (loop with start = 0
|
||||
for pos = (position i seq :start start)
|
||||
until pos
|
||||
do (setf start (length seq)
|
||||
seq (stern-brocot seq))
|
||||
finally (return (1+ pos)))
|
||||
do (format t "First ~D at ~D~%" i index)))
|
||||
|
||||
(defun first-100 ()
|
||||
(loop for input = nil then seq
|
||||
for start = (length input)
|
||||
for seq = (stern-brocot input)
|
||||
for pos = (position 100 seq :start start)
|
||||
until pos
|
||||
finally (format t "First 100 at ~D~%" (1+ pos))))
|
||||
|
||||
(defun check-gcd ()
|
||||
(loop for input = nil then seq
|
||||
for seq = (stern-brocot input)
|
||||
while (< (length seq) 1000)
|
||||
finally (if (loop for i from 0 below 999
|
||||
always (= 1 (gcd (aref seq i) (aref seq (1+ i)))))
|
||||
(write-line "Correct. The GCDs of all the two consecutive numbers are 1.")
|
||||
(write-line "Wrong."))))
|
||||
|
||||
(defun main ()
|
||||
(first-15)
|
||||
(first-1-to-10)
|
||||
(first-100)
|
||||
(check-gcd))
|
||||
|
|
@ -1,9 +1,14 @@
|
|||
import Data.List
|
||||
import Data.List (elemIndex)
|
||||
|
||||
sb = 1:1: f (tail sb) sb where
|
||||
f (a:aa) (b:bb) = a+b : a : f aa bb
|
||||
sb :: [Int]
|
||||
sb = 1 : 1 : f (tail sb) sb
|
||||
where
|
||||
f (a:aa) (b:bb) = a + b : a : f aa bb
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
print $ take 15 sb
|
||||
print [(i,1 + (\(Just i)->i) (elemIndex i sb)) | i <- [1..10]++[100]]
|
||||
print $ all (\(a,b)->1 == gcd a b) $ take 1000 $ zip sb (tail sb)
|
||||
print $ take 15 sb
|
||||
print
|
||||
[ (i, 1 + (\(Just i) -> i) (elemIndex i sb))
|
||||
| i <- [1 .. 10] ++ [100] ]
|
||||
print $ all (\(a, b) -> 1 == gcd a b) $ take 1000 $ zip sb (tail sb)
|
||||
|
|
|
|||
24
Task/Stern-Brocot-sequence/Julia/stern-brocot-sequence.julia
Normal file
24
Task/Stern-Brocot-sequence/Julia/stern-brocot-sequence.julia
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
function sternbrocot(f::Function=(x) -> length(x) ≥ 20)::Vector{Int}
|
||||
rst = Int[1, 1]
|
||||
i = 3
|
||||
while !f(rst)
|
||||
append!(rst, Int[rst[i-1] + rst[i-2], rst[i-2]])
|
||||
i += 1
|
||||
end
|
||||
return rst
|
||||
end
|
||||
|
||||
println("First 15 elements of Stern-Brocot series:\n", sternbrocot(x -> length(x) ≥ 15)[1:15], "\n")
|
||||
|
||||
for i in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100)
|
||||
occurr = findfirst(x -> x == i, sternbrocot(x -> i ∈ x))
|
||||
@printf("Index of first occurrence of %3i in the series: %4i\n", i, occurr)
|
||||
end
|
||||
|
||||
print("\nAssertion: the greatest common divisor of all the two\nconsecutive members of the series up to the 1000th member, is always one: ")
|
||||
sb = sternbrocot(x -> length(x) > 1000)
|
||||
if all(gcd(prev, this) == 1 for (prev, this) in zip(sb[1:1000], sb[2:1000]))
|
||||
println("Confirmed.")
|
||||
else
|
||||
println("Rejected.")
|
||||
end
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
// version 1.1.0
|
||||
|
||||
val sbs = mutableListOf(1, 1)
|
||||
|
||||
fun sternBrocot(n: Int, fromStart: Boolean = true) {
|
||||
if (n < 4 || (n % 2 != 0)) throw IllegalArgumentException("n must be >= 4 and even")
|
||||
var consider = if (fromStart) 1 else n / 2 - 1
|
||||
while (true) {
|
||||
val sum = sbs[consider] + sbs[consider - 1]
|
||||
sbs.add(sum)
|
||||
sbs.add(sbs[consider])
|
||||
if (sbs.size == n) break
|
||||
consider++
|
||||
}
|
||||
}
|
||||
|
||||
fun gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var n = 16 // needs to be even to ensure 'considered' number is added
|
||||
println("First 15 members of the Stern-Brocot sequence")
|
||||
sternBrocot(n)
|
||||
println(sbs.take(15))
|
||||
|
||||
val firstFind = IntArray(11) // all zero by default
|
||||
firstFind[0] = -1 // needs to be non-zero for subsequent test
|
||||
for ((i, v) in sbs.withIndex())
|
||||
if (v <= 10 && firstFind[v] == 0) firstFind[v] = i + 1
|
||||
loop@ while (true) {
|
||||
n += 2
|
||||
sternBrocot(n, false)
|
||||
val vv = sbs.takeLast(2)
|
||||
var m = n - 1
|
||||
for (v in vv) {
|
||||
if (v <= 10 && firstFind[v] == 0) firstFind[v] = m
|
||||
if (firstFind.all { it != 0 }) break@loop
|
||||
m++
|
||||
}
|
||||
}
|
||||
println("\nThe numbers 1 to 10 first appear at the following indices:")
|
||||
for (i in 1..10) println("${"%2d".format(i)} -> ${firstFind[i]}")
|
||||
|
||||
print("\n100 first appears at index ")
|
||||
while (true) {
|
||||
n += 2
|
||||
sternBrocot(n, false)
|
||||
val vv = sbs.takeLast(2)
|
||||
if (vv[0] == 100) {
|
||||
println(n - 1); break
|
||||
}
|
||||
if (vv[1] == 100) {
|
||||
println(n); break
|
||||
}
|
||||
}
|
||||
|
||||
print("\nThe GCDs of each pair of the series up to the 1000th member are ")
|
||||
for (p in 0..998 step 2) {
|
||||
if (gcd(sbs[p], sbs[p + 1]) != 1) {
|
||||
println("not all one")
|
||||
return
|
||||
}
|
||||
}
|
||||
println("all one")
|
||||
}
|
||||
|
|
@ -1,12 +1,11 @@
|
|||
constant Stern-Brocot = flat
|
||||
1, 1, -> *@a {
|
||||
@a[$_ - 1] + @a[$_], @a[$_] given ++$;
|
||||
} ... *;
|
||||
|
||||
say Stern-Brocot[^15];
|
||||
|
||||
for 1 .. 10, 100 -> $ix {
|
||||
say "first occurrence of $ix is at index : ", 1 + Stern-Brocot.first($ix, :k);
|
||||
constant @Stern-Brocot = 1, 1, {
|
||||
|(@_[$_ - 1] + @_[$_], @_[$_]) given ++$
|
||||
} ... *;
|
||||
|
||||
say @Stern-Brocot[^15];
|
||||
|
||||
for (flat 1..10, 100) -> $ix {
|
||||
say "first occurrence of $ix is at index : ", 1 + @Stern-Brocot.first($ix, :k);
|
||||
}
|
||||
|
||||
say so 1 == all map ^1000: { [gcd] Stern-Brocot[$_, $_ + 1] }
|
||||
|
||||
say so 1 == all map ^1000: { [gcd] @Stern-Brocot[$_, $_ + 1] }
|
||||
|
|
|
|||
41
Task/Stern-Brocot-sequence/Phix/stern-brocot-sequence.phix
Normal file
41
Task/Stern-Brocot-sequence/Phix/stern-brocot-sequence.phix
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
sequence sb = {1,1}
|
||||
integer c = 2
|
||||
|
||||
function stern_brocot(integer n)
|
||||
while length(sb)<n do
|
||||
sb &= sb[c]+sb[c-1] & sb[c]
|
||||
c += 1
|
||||
end while
|
||||
return sb[1..n]
|
||||
end function
|
||||
|
||||
sequence s = stern_brocot(15)
|
||||
puts(1,"first 15:")
|
||||
?s
|
||||
integer n = 16, k
|
||||
sequence idx = tagset(10)
|
||||
for i=1 to length(idx) do
|
||||
while 1 do
|
||||
k = find(idx[i],s)
|
||||
if k!=0 then exit end if
|
||||
n *= 2
|
||||
s = stern_brocot(n)
|
||||
end while
|
||||
idx[i] = k
|
||||
end for
|
||||
puts(1,"indexes of 1..10:")
|
||||
?idx
|
||||
puts(1,"index of 100:")
|
||||
while 1 do
|
||||
k = find(100,s)
|
||||
if k!=0 then exit end if
|
||||
n *= 2
|
||||
s = stern_brocot(n)
|
||||
end while
|
||||
?k
|
||||
s = stern_brocot(1000)
|
||||
integer maxgcd = 1
|
||||
for i=1 to 999 do
|
||||
maxgcd = max(gcd(s[i],s[i+1]),maxgcd)
|
||||
end for
|
||||
printf(1,"max gcd:%d\n",{maxgcd})
|
||||
22
Task/Stern-Brocot-sequence/R/stern-brocot-sequence.r
Normal file
22
Task/Stern-Brocot-sequence/R/stern-brocot-sequence.r
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
## Stern-Brocot sequence
|
||||
## 12/19/16 aev
|
||||
SternBrocot <- function(n){
|
||||
V <- 1; k <- n/2;
|
||||
for (i in 1:k)
|
||||
{ V[2*i] = V[i]; V[2*i+1] = V[i] + V[i+1];}
|
||||
return(V);
|
||||
}
|
||||
|
||||
## Required tests:
|
||||
require(pracma);
|
||||
{
|
||||
cat(" *** The first 15:",SternBrocot(15),"\n");
|
||||
cat(" *** The first i@n:","\n");
|
||||
V=SternBrocot(40);
|
||||
for (i in 1:10) {j=match(i,V); cat(i,"@",j,",")}
|
||||
V=SternBrocot(1200);
|
||||
i=100; j=match(i,V); cat(i,"@",j,"\n");
|
||||
V=SternBrocot(1000); j=1;
|
||||
for (i in 2:1000) {j=j*gcd(V[i-1],V[i])}
|
||||
if(j==1) {cat(" *** All GCDs=1!\n")} else {cat(" *** All GCDs!=1??\n")}
|
||||
}
|
||||
|
|
@ -1,34 +1,34 @@
|
|||
# Declare a function to generate the Stern-Brocot sequence
|
||||
func stern_brocot {
|
||||
var list = [1, 1];
|
||||
var list = [1, 1]
|
||||
func {
|
||||
list.append(list[0]+list[1], list[1]);
|
||||
list.shift;
|
||||
list.append(list[0]+list[1], list[1])
|
||||
list.shift
|
||||
}
|
||||
}
|
||||
|
||||
# Show the first fifteen members of the sequence.
|
||||
1..15 -> map(stern_brocot()).join(' ').say;
|
||||
say 15.of(stern_brocot()).join(' ')
|
||||
|
||||
# Show the (1-based) index of where the numbers 1-to-10 first appears
|
||||
# in the sequence, and where the number 100 first appears in the sequence.
|
||||
[(1..10)..., 100].each { |i|
|
||||
var index = 1;
|
||||
var generator = stern_brocot();
|
||||
while (generator() != i) {
|
||||
++index;
|
||||
for i (1..10, 100) {
|
||||
var index = 1
|
||||
var generator = stern_brocot()
|
||||
while (generator() != i) {
|
||||
++index
|
||||
}
|
||||
say "First occurrence of #{i} is at index #{index}";
|
||||
say "First occurrence of #{i} is at index #{index}"
|
||||
}
|
||||
|
||||
# Check that the greatest common divisor of all the two consecutive
|
||||
# members of the series up to the 1000th member, is always one.
|
||||
var generator = stern_brocot();
|
||||
var (a, b) = (generator(), generator());
|
||||
var generator = stern_brocot()
|
||||
var (a, b) = (generator(), generator())
|
||||
{
|
||||
assert_eq(Math.gcd(a, b), 1);
|
||||
a = b;
|
||||
b = generator();
|
||||
} * 1000;
|
||||
assert_eq(gcd(a, b), 1)
|
||||
a = b
|
||||
b = generator()
|
||||
} * 1000
|
||||
|
||||
say "All GCD's are 1";
|
||||
say "All GCD's are 1"
|
||||
|
|
|
|||
11
Task/Stern-Brocot-sequence/Zkl/stern-brocot-sequence.zkl
Normal file
11
Task/Stern-Brocot-sequence/Zkl/stern-brocot-sequence.zkl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fcn SB // Stern-Brocot sequence factory --> Walker
|
||||
{ Walker(fcn(sb,n){ a,b:=sb; sb.append(a+b,b); sb.del(0); a }.fp(L(1,1))) }
|
||||
|
||||
SB().walk(15).println();
|
||||
|
||||
[1..10].zipWith('wrap(n){ [1..].zip(SB())
|
||||
.filter(1,fcn(n,sb){ n==sb[1] }.fp(n)) })
|
||||
.walk().println();
|
||||
[1..].zip(SB()).filter1(fcn(sb){ 100==sb[1] }).println();
|
||||
|
||||
sb:=SB(); do(500){ if(sb.next().gcd(sb.next())!=1) println("Oops") }
|
||||
Loading…
Add table
Add a link
Reference in a new issue