Skip to content

Instantly share code, notes, and snippets.

@sebastianv89
Created June 6, 2021 09:14
Show Gist options
  • Save sebastianv89/8178298ec2ec7b35aa4088ba7ecdad72 to your computer and use it in GitHub Desktop.
Save sebastianv89/8178298ec2ec7b35aa4088ba7ecdad72 to your computer and use it in GitHub Desktop.
# From u/42IsHoly:
# https://www.reddit.com/r/math/comments/nt4o10/a_pretty_cool_but_completely_useless_function_i/
#
# > we can define a “pseudo-base function” which we’ll call B. B(x) = Sum (xn *
# > nth digit of x) Where the ones digit is the 0th, the tens digit is the 1st,
# > the 1/10s digit the -1st the hundreds digit the 2nd, etc.
#
# This function assumes conversion from the decimal base, but we should be able to generalize that.
#
# Script by u/toppletwist. 2021, public domain.
import matplotlib.pyplot as plt
import numpy as np
import math
MAX_TERMS = 32
def to_base(base, x):
a = []
i = 0
while x > 0 and i < MAX_TERMS:
x, n = math.modf(x)
a.append(n)
x *= base
i += 1
return a
def pretty_cool(base, x):
assert (x > 0 and x < 1), "n should be in open interval (0,1)"
res = 0.
b = 1.
for digit in to_base(base, x):
res += b * digit
b = b / x
return res
STEP_SIZE = .001
BASES = [1.1, 2, math.e, math.pi, 4, 8, 10, 12, 16, 32, 60]
xs = np.arange(STEP_SIZE, 1., STEP_SIZE)
for base in BASES:
ys = list(map(lambda n: pretty_cool(base, n), xs))
plt.plot(xs, ys, label=f"base {base}")
plt.legend()
plt.yscale('log')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment