A few fun little number theory concepts i've messed with over the last few days.

Demlo Numbershttp://mathworld.wolfram.com/DemloNumber.htmlhttps://github.com/adarqui/Daimyo/blob/master/Haskell/src/Daimyo/NumberTheory/Demlo.hsPretty cool how the first 2 through 9 demlo numbers are palindromic:

`demlos = tail [ x*x | x <- demlo'ones ]`

demlo'ones = scanl (\acc x -> acc + x) 1 by10

`*Daimyo> import Daimyo.NumberTheory.Demlo `

*Daimyo Daimyo.NumberTheory.Demlo> take 8 demlos

[121,12321,1234321,123454321,12345654321,1234567654321,123456787654321,12345678987654321]

*Daimyo Daimyo.NumberTheory.Demlo>

Palindromic numbers by squaring 11, 111, 11111, 111111, 1111111, 11111111, ... until 111111111

`*Daimyo Daimyo.NumberTheory.Demlo> tail $ take 9 demlo'ones`

[11,111,1111,11111,111111,1111111,11111111,111111111]

Continued Fractionshttp://en.wikipedia.org/wiki/Continued_fractionhttps://github.com/adarqui/Daimyo/blob/master/Haskell/src/Daimyo/NumberTheory/ContinuedFraction.hsOften times you see patterns such as "e" or the "golden ratio" represented by some kind of formula.

https://github.com/adarqui/Daimyo/blob/master/Haskell/src/Daimyo/NumberTheory/Constants/E.hse'precision sums the sequence (e'sequence) from 1 to n of 1/n!

`*Daimyo> import Daimyo.NumberTheory.Constants.E`

*Daimyo Daimyo.NumberTheory.Constants.E> take 10 $ e'sequence 10

[1.0,1.0,0.5,0.16666666666666666,4.1666666666666664e-2,8.333333333333333e-3,1.388888888888889e-3,1.984126984126984e-4,2.48015873015873e-5,2.7557319223985893e-6]

The sum of that sequence of reciprocal factorials ends up approaching e.

As for golden ratio, I always see it calculated as so: (1 + (sqrt 5)) / 2

https://github.com/adarqui/Daimyo/blob/master/Haskell/src/Daimyo/NumberTheory/GoldenRatio.hsThe continued fraction style calculations are interesting. I will be using the 'abbreviated form' of continued fractions which is [a0;a1,a2,a3,...an] which is equivalent to:

`a0 + 1 / (a1 + 1 / (a2 + 1 / ... (1 / an)))`

Finally, here is a link to some example constants:

https://github.com/adarqui/Daimyo/blob/master/Haskell/src/Daimyo/NumberTheory/ContinuedFraction/Constant.hsee = [2;1,2,1,1,4,1,1,6,1,1,8,…] (sequence A003417 in OEIS). The pattern repeats indefinitely with a period of 3 except that 2 is added to one of the terms in each cycle.

To see that pattern, you can produce the cycle like so:

https://github.com/adarqui/Daimyo/blob/master/Haskell/src/Daimyo/NumberTheory/Constants/E.hs`e'continuedFraction'abbreviatedForm precision =`

let

cycle = concat $ [1,2] : [ [1,1,x+2] | x <- [2,4..precision] ]

in

(2, cycle)

`(2,[1,2,1,1,4,1,1,6,1,1,8,1,1,10,1,1,12,1,1,14,1,1,16,1,1,18,1,1,20,1,1,22,1,1,24,1,1,26,1,1,28,1,1,30,1,1,32,1,1,34,1,1,36,1,1,38,1,1,40,1,1,42,1,1,44,1,1,46,1,1,48,1,1,50,1,1,52,1,1,54,1,1,56,1,1,58,1,1,60,1,1,62,1,1,64,1,1,66,1,1,68,1,1,70,1,1,72,1,1,74,1,1,76,1,1,78,1,1,80,1,1,82,1,1,84,1,1,86,1,1,88,1,1,90,1,1,92,1,1,94,1,1,96,1,1,98,1,1,100,1,1,102])`

^^ produces the pattern in the quote above.

`*Daimyo Daimyo.NumberTheory.Constants.E> e'continuedFraction'abbreviatedForm'evaluated 100`

2.7182818284590455

Golden RatioEntire books have been written about the golden ratio and it's properties as it's found in nature & various phenomenon.

The continued fraction approach to calculating the golden ratio is s follows:

ϕ = [1;1,1,1,1,1,1,1,1,1,1,1,…]

`_goldenRatioN precision = continuedFraction (1, replicate precision 1)`

`*Daimyo Daimyo.NumberTheory.ContinuedFraction.Constant> _goldenRatioN 1000`

1.618033988749895

piThere's no continued fraction pattern for calculating pi (edit: with numerators of 1). If you find it, message me. Thnx.

If you don't restrict the numerators to 1's, then ya:

http://functions.wolfram.com/Constants/Pi/10/0002/quick:

Continued fractions with arbitrary numerators:

`{-`

allow numerators to be specified

-}

continuedFractionNumerators (n,nl,dl) = n + continuedFractionNumerators'rest nl dl

continuedFractionNumerators'rest (n:ns) ds = n / continuedFractionNumerators'rest' ns ds

continuedFractionNumerators'rest' _ [] = 0

continuedFractionNumerators'rest' [] _ = 0

continuedFractionNumerators'rest' (n:ns) (d:ds) = d + (n / continuedFractionNumerators'rest' ns ds)

pi continued fraction calculation:

`pi'continuedFraction :: Integer -> Double`

pi'continuedFraction precision =

let

a's = replicate (fromIntegral precision :: Int) 6

numerators = [ fromIntegral (x*x) :: Double | x <- odds ]

in

continuedFractionNumerators (3, numerators, a's)

`pi'continuedFraction 10000`

3.141592653590043

boom.

pC