sundries

Assorted little morsels for your enjoyment

git clone https://code.pdelong.com/sundries.git

 1function nextTriangle(prev::Array{BigInt})
 2    next = ones(BigInt, length(prev)+1)
 3    for k in 2:(length(next)-1)
 4        next[k] = prev[k-1] + prev[k];
 5    end
 6
 7    return next;
 8end
 9
10function nthIterateTriangle(n::Int)
11    curr = [BigInt(1)]
12    for i in 1:n
13        curr = nextTriangle(curr)
14    end
15
16    return curr
17end
18
19function nthClosedTriangle(n::Int)
20    row = zeros(BigInt, n+1)
21    for i in 0:n
22        row[i+1] = binomial(big(n), big(i))
23    end
24
25    return row
26end
27
28function numOdd(row::Array{BigInt})
29    numOdd = 0
30    for num in row
31        if num % 2 == 1
32            numOdd += 1
33        end
34    end
35
36    return big(numOdd)
37end
38
39function closedNumOdd(n::Int)
40    row = nthClosedTriangle(n)
41
42    numOdd = 0
43    for num in row
44        if num % 2 == 1
45            numOdd += 1
46        end
47    end
48
49    return big(numOdd)
50end
51
52function closedFracOdd(n::Int)
53    odd = zeros(BigInt, Threads.nthreads())
54    Threads.@threads for i in 0:n
55        odd[Threads.threadid()] += closedNumOdd(i)
56    end
57
58    total = reduce(+, odd)
59    num = big(n+1)
60    totalNums = num*(num+1)//2
61
62    return total // totalNums
63end
64
65function fracOdd(n::Int)
66    curr = [big(1)]
67    odd = numOdd(curr)
68    for i in 1:n
69        curr = nextTriangle(curr)
70        odd += numOdd(curr)
71    end
72
73    num = big(n+1)
74    totalNums = num*(num+1)//2
75
76    return odd // totalNums
77end
78
79println(fracOdd(127))