def invcdf(p): """ Inverse of the CDF of the raised cosine distribution. """ with mpmath.extradps(5): p = mpmath.mpf(p) if p < 0 or p > 1: return mpmath.nan if p == 0: return -mpmath.pi if p == 1: return mpmath.pi if p < 0.094: x = _poly_approx(mpmath.cbrt(12 * mpmath.pi * p)) - mpmath.pi elif p > 0.906: x = mpmath.pi - _poly_approx(mpmath.cbrt(12 * mpmath.pi * (1 - p))) else: y = mpmath.pi * (2 * p - 1) y2 = y**2 x = y * _p2(y2) / _q2(y2) solver = 'mnewton' x = mpmath.findroot(f=lambda t: cdf(t) - p, x0=x, df=lambda t: (1 + mpmath.cos(t)) / (2 * mpmath.pi), df2=lambda t: -mpmath.sin(t) / (2 * mpmath.pi), solver=solver) return x
def __compute_roots(self,a,c,d): from mpmath import mpf, mpc, sqrt, cbrt assert(all([isinstance(_,mpf) for _ in [a,c,d]])) Delta = self.__Delta # NOTE: this was the original function used for root finding. # proots, err = polyroots([a,0,c,d],error=True,maxsteps=5000000) # Computation of the cubic roots. # TODO special casing. u_list = [mpf(1),mpc(-1,sqrt(3))/2,mpc(-1,-sqrt(3))/2] Delta0 = -3 * a * c Delta1 = 27 * a * a * d C1 = cbrt((Delta1 + sqrt(Delta1 * Delta1 - 4 * Delta0 * Delta0 * Delta0)) / 2) C2 = cbrt((Delta1 - sqrt(Delta1 * Delta1 - 4 * Delta0 * Delta0 * Delta0)) / 2) if abs(C1) > abs(C2): C = C1 else: C = C2 proots = [(-1 / (3 * a)) * (u * C + Delta0 / (u * C)) for u in u_list] # NOTE: we ignore any residual imaginary part that we know must come from numerical artefacts. if Delta < 0: # Sort the roots following the convention: complex with negative imaginary, real, complex with positive imaginary. # Then assign the roots following the P convention (e2 real root, e1 complex with positive imaginary). e3,e2,e1 = sorted(proots,key = lambda c: c.imag) else: # The convention in this case is to sort in descending order. e1,e2,e3 = sorted([_.real for _ in proots],reverse = True) return e1,e2,e3
def __cubic_roots(self,a,c,d): from mpmath import mpf, mpc, sqrt, cbrt assert(all([isinstance(_,mpf) for _ in [a,c,d]])) Delta = -4 * a * c*c*c - 27 * a*a * d*d self.__Delta = Delta # NOTE: this was the original function used for root finding. # proots, err = polyroots([a,0,c,d],error=True,maxsteps=5000000) # Computation of the cubic roots. u_list = [mpf(1),mpc(-1,sqrt(3))/2,mpc(-1,-sqrt(3))/2] Delta0 = -3 * a * c Delta1 = 27 * a * a * d # Here we have two choices for the value from sqrt, positive or negative. Since C # is used as a denominator below, we pick the choice with the greatest absolute value. # http://en.wikipedia.org/wiki/Cubic_function # This should handle the case g2 = 0 gracefully. C1 = cbrt((Delta1 + sqrt(Delta1 * Delta1 - 4 * Delta0 * Delta0 * Delta0)) / 2) C2 = cbrt((Delta1 - sqrt(Delta1 * Delta1 - 4 * Delta0 * Delta0 * Delta0)) / 2) if abs(C1) > abs(C2): C = C1 else: C = C2 proots = [(-1 / (3 * a)) * (u * C + Delta0 / (u * C)) for u in u_list] # NOTE: we ignore any residual imaginary part that we know must come from numerical artefacts. if Delta < 0: # Sort the roots following the convention: complex with negative imaginary, real, complex with positive imaginary. # Then assign the roots following the P convention (e2 real root, e1 complex with positive imaginary). e3,e2,e1 = sorted(proots,key = lambda c: c.imag) else: # The convention in this case is to sort in descending order. e1,e2,e3 = sorted([_.real for _ in proots],reverse = True) return e1,e2,e3
def __cubic_roots(self, a, c, d): from mpmath import mpf, mpc, sqrt, cbrt assert (all([isinstance(_, mpf) for _ in [a, c, d]])) Delta = -4 * a * c * c * c - 27 * a * a * d * d self.__Delta = Delta # NOTE: this was the original function used for root finding. # proots, err = polyroots([a,0,c,d],error=True,maxsteps=5000000) # Computation of the cubic roots. u_list = [mpf(1), mpc(-1, sqrt(3)) / 2, mpc(-1, -sqrt(3)) / 2] Delta0 = -3 * a * c Delta1 = 27 * a * a * d # Here we have two choices for the value from sqrt, positive or negative. Since C # is used as a denominator below, we pick the choice with the greatest absolute value. # http://en.wikipedia.org/wiki/Cubic_function # This should handle the case g2 = 0 gracefully. C1 = cbrt( (Delta1 + sqrt(Delta1 * Delta1 - 4 * Delta0 * Delta0 * Delta0)) / 2) C2 = cbrt( (Delta1 - sqrt(Delta1 * Delta1 - 4 * Delta0 * Delta0 * Delta0)) / 2) if abs(C1) > abs(C2): C = C1 else: C = C2 proots = [(-1 / (3 * a)) * (u * C + Delta0 / (u * C)) for u in u_list] # NOTE: we ignore any residual imaginary part that we know must come from numerical artefacts. if Delta < 0: # Sort the roots following the convention: complex with negative imaginary, real, complex with positive imaginary. # Then assign the roots following the P convention (e2 real root, e1 complex with positive imaginary). e3, e2, e1 = sorted(proots, key=lambda c: c.imag) else: # The convention in this case is to sort in descending order. e1, e2, e3 = sorted([_.real for _ in proots], reverse=True) return e1, e2, e3
def DedekindEtaA4(tau): ''' Compute the derivative of the Dedekind Eta function for imaginary argument tau. Numerically. ''' try: import mpmath as mp mpmath_loaded = True except ImportError: mpmath_loaded = False return mp.cbrt(0.5*mp.jtheta(2,0,mp.exp(-mp.pi*tau))*mp.jtheta(3,0,mp.exp(-mp.pi*tau))*mp.jtheta(4,0,mp.exp(-mp.pi*tau)))
def DedekindEtaA(tau): ''' Alternative definition of the Dedekind Eta function in terms of the Jacobi theta function. 10x faster computation. http://functions.wolfram.com/EllipticFunctions/DedekindEta/27/01/02/''' try: import mpmath as mp mpmath_loaded = True except ImportError: mpmath_loaded = False return mp.cbrt(0.5*mp.jtheta(1,0,mp.exp(-mp.pi*tau),1))
def describeIntegerOperator(n): indent = ' ' * 4 print() print(int(n), 'is:') if isOdd(n): print(indent + 'odd') elif isEven(n): print(indent + 'even') if isPrime(n): isPrimeFlag = True print(indent + 'prime') elif n > 3: isPrimeFlag = False print(indent + 'composite') else: isPrimeFlag = False if isKthPower(n, 2): print(indent + 'the ' + getShortOrdinalName(sqrt(n)) + ' square number') if isKthPower(n, 3): print(indent + 'the ' + getShortOrdinalName(cbrt(n)) + ' cube number') for i in arange(4, fadd(ceil(log(fabs(n), 2)), 1)): if isKthPower(n, i): print(indent + 'the ' + getShortOrdinalName(root(n, i)) + ' ' + getNumberName(i, True) + ' power') # triangular guess = findPolygonalNumber(n, 3) if getNthPolygonalNumber(guess, 3) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' triangular number') # pentagonal guess = findPolygonalNumber(n, 5) if getNthPolygonalNumber(guess, 5) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' pentagonal number') # hexagonal guess = findPolygonalNumber(n, 6) if getNthPolygonalNumber(guess, 6) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' hexagonal number') # heptagonal guess = findPolygonalNumber(n, 7) if getNthPolygonalNumber(guess, 7) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' heptagonal number') # octagonal guess = findPolygonalNumber(n, 8) if getNthPolygonalNumber(guess, 8) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' octagonal number') # nonagonal guess = findPolygonalNumber(n, 9) if getNthPolygonalNumber(guess, 9) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' nonagonal number') # decagonal guess = findPolygonalNumber(n, 10) if getNthPolygonalNumber(guess, 10) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' decagonal number') # if n > 1: # for i in range( 11, 101 ): # if getNthPolygonalNumber( findPolygonalNumber( n, i ), i ) == n: # print( indent + str( i ) + '-gonal' ) # centered triangular guess = findCenteredPolygonalNumber(n, 3) if getNthCenteredPolygonalNumber(guess, 3) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' centered triangular') # centered square guess = findCenteredPolygonalNumber(n, 4) if getNthCenteredPolygonalNumber(guess, 4) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' centered square number') # centered pentagonal guess = findCenteredPolygonalNumber(n, 5) if getNthCenteredPolygonalNumber(guess, 5) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' centered pentagonal number') # centered hexagonal guess = findCenteredPolygonalNumber(n, 6) if getNthCenteredPolygonalNumber(guess, 6) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' centered hexagonal number') # centered heptagonal guess = findCenteredPolygonalNumber(n, 7) if getNthCenteredPolygonalNumber(guess, 7) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' centered heptagonal number') # centered octagonal guess = findCenteredPolygonalNumber(n, 8) if getNthCenteredPolygonalNumber(guess, 8) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' centered octagonal number') # centered nonagonal guess = findCenteredPolygonalNumber(n, 9) if getNthCenteredPolygonalNumber(guess, 9) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' centered nonagonal number') # centered decagonal guess = findCenteredPolygonalNumber(n, 10) if getNthCenteredPolygonalNumber(guess, 10) == n: print(indent + 'the ' + getShortOrdinalName(guess) + ' centered decagonal number') # pandigital if isPandigital(n): print(indent + 'pandigital') # for i in range( 4, 21 ): # if isBaseKPandigital( n, i ): # print( indent + 'base ' + str( i ) + ' pandigital' ) # Fibonacci result = findInput(n, fib, lambda n: fmul(log10(n), 5)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Fibonacci number') # Tribonacci result = findInput(n, lambda n: getNthKFibonacciNumber(n, 3), lambda n: fmul(log10(n), 5)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Tribonacci number') # Tetranacci result = findInput(n, lambda n: getNthKFibonacciNumber(n, 4), lambda n: fmul(log10(n), 5)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Tetranacci number') # Pentanacci result = findInput(n, lambda n: getNthKFibonacciNumber(n, 5), lambda n: fmul(log10(n), 5)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Pentanacci number') # Hexanacci result = findInput(n, lambda n: getNthKFibonacciNumber(n, 6), lambda n: fmul(log10(n), 5)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Hexanacci number') # Heptanacci result = findInput(n, lambda n: getNthKFibonacciNumber(n, 7), lambda n: fmul(log10(n), 5)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Heptanacci number') # Octanacci result = findInput(n, lambda n: getNthKFibonacciNumber(n, 8), lambda n: fmul(log10(n), 5)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Octanacci number') # Lucas numbers result = findInput(n, getNthLucasNumber, lambda n: fmul(log10(n), 5)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Lucas number') # base-k repunits if n > 1: for i in range(2, 21): result = findInput(n, lambda x: getNthBaseKRepunit(x, i), lambda n: log(n, i), minimum=1) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' base-' + str(i) + ' repunit') # Jacobsthal numbers result = findInput(n, getNthJacobsthalNumber, lambda n: fmul(log(n), 1.6)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Jacobsthal number') # Padovan numbers result = findInput(n, getNthPadovanNumber, lambda n: fmul(log10(n), 9)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Padovan number') # Fibonorial numbers result = findInput(n, getNthFibonorial, lambda n: sqrt(fmul(log(n), 10))) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Fibonorial number') # Mersenne primes result = findInput(n, getNthMersennePrime, lambda n: fadd(fmul(log(log(sqrt(n))), 2.7), 3)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Mersenne prime') # perfect number result = findInput(n, getNthPerfectNumber, lambda n: fmul(log(log(n)), 2.6)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' perfect number') # Mersenne exponent result = findInput(n, getNthMersenneExponent, lambda n: fmul(log(n), 2.7), maximum=50) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Mersenne exponent') if not isPrimeFlag and n != 1 and n <= LARGEST_NUMBER_TO_FACTOR: # deficient if isDeficient(n): print(indent + 'deficient') # abundant if isAbundant(n): print(indent + 'abundant') # k_hyperperfect for i in sorted(list(set(sorted( downloadOEISSequence(34898)[:500]))))[1:]: if i > n: break if isKHyperperfect(n, i): print(indent + str(i) + '-hyperperfect') break # smooth for i in getPrimes(2, 50): if isSmooth(n, i): print(indent + str(i) + '-smooth') break # rough previousPrime = 2 for i in getPrimes(2, 50): if not isRough(n, i): print(indent + str(previousPrime) + '-rough') break previousPrime = i # is_semiprime if isSemiprime(n): print(indent + 'semiprime') # is_sphenic elif isSphenic(n): print(indent + 'sphenic') elif isSquareFree(n): print(indent + 'square-free') # powerful if isPowerful(n): print(indent + 'powerful') # factorial result = findInput(n, getNthFactorial, lambda n: power(log10(n), 0.92)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' factorial number') # alternating factorial result = findInput(n, getNthAlternatingFactorial, lambda n: fmul(sqrt(log(n)), 0.72)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' alternating factorial number') # double factorial if n == 1: result = (True, 1) else: result = findInput(n, getNthDoubleFactorial, lambda n: fdiv(power(log(log(n)), 4), 7)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' double factorial number') # hyperfactorial result = findInput(n, getNthHyperfactorial, lambda n: fmul(sqrt(log(n)), 0.8)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' hyperfactorial number') # subfactorial result = findInput(n, getNthSubfactorial, lambda n: fmul(log10(n), 1.1)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' subfactorial number') # superfactorial if n == 1: result = (True, 1) else: result = findInput(n, getNthSuperfactorial, lambda n: fadd(sqrt(log(n)), 1)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' superfactorial number') # pernicious if isPernicious(n): print(indent + 'pernicious') # pronic if isPronic(n): print(indent + 'pronic') # Achilles if isAchillesNumber(n): print(indent + 'an Achilles number') # antiharmonic if isAntiharmonic(n): print(indent + 'antiharmonic') # unusual if isUnusual(n): print(indent + 'unusual') # hyperperfect for i in range(2, 21): if isKHyperperfect(n, i): print(indent + str(i) + '-hyperperfect') # Ruth-Aaron if isRuthAaronNumber(n): print(indent + 'a Ruth-Aaron number') # Smith numbers if isSmithNumber(n): print(indent + 'a Smith number') # base-k Smith numbers for i in range(2, 10): if isBaseKSmithNumber(n, i): print(indent + 'a base-' + str(i) + ' Smith number') # order-k Smith numbers for i in range(2, 11): if isOrderKSmithNumber(n, i): print(indent + 'an order-' + str(i) + ' Smith number') # polydivisible if isPolydivisible(n): print(indent + 'polydivisible') # Carol numbers result = findInput(n, getNthCarolNumber, lambda n: fmul(log10(n), fdiv(5, 3))) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Carol number') # Kynea numbers result = findInput(n, getNthKyneaNumber, lambda n: fmul(log10(n), fdiv(5, 3))) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Kynea number') # Leonardo numbers result = findInput(n, getNthLeonardoNumber, lambda n: fsub(log(n, phi), fdiv(1, phi))) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Leonardo number') # Thabit numbers result = findInput(n, getNthThabitNumber, lambda n: fmul(log10(n), 3.25)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Thabit number') # Carmmichael if isCarmichaelNumber(n): print(indent + 'a Carmichael number') # narcissistic if isNarcissistic(n): print(indent + 'narcissistic') # PDI if isPerfectDigitalInvariant(n): print(indent + 'a perfect digital invariant') # PDDI if isPerfectDigitToDigitInvariant(n, 10): print(indent + 'a perfect digit-to-digit invariant in base 10') # Kaprekar if isKaprekarNumber(n): print(indent + 'a Kaprekar number') # automorphic if isAutomorphic(n): print(indent + 'automorphic') # trimorphic if isTrimorphic(n): print(indent + 'trimorphic') # k-morphic for i in range(4, 11): if isKMorphic(n, i): print(indent + str(i) + '-morphic') # bouncy if isBouncy(n): print(indent + 'bouncy') # step number if isStepNumber(n): print(indent + 'a step number') # Apery numbers result = findInput(n, getNthAperyNumber, lambda n: fadd(fdiv(log(n), 1.5), 1)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Apery number') # Delannoy numbers result = findInput(n, getNthDelannoyNumber, lambda n: fmul(log10(n), 1.35)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Delannoy number') # Schroeder numbers result = findInput(n, getNthSchroederNumber, lambda n: fmul(log10(n), 1.6)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Schroeder number') # Schroeder-Hipparchus numbers result = findInput(n, getNthSchroederHipparchusNumber, lambda n: fdiv(log10(n), 1.5)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Schroeder-Hipparchus number') # Motzkin numbers result = findInput(n, getNthMotzkinNumber, log) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Motzkin number') # Pell numbers result = findInput(n, getNthPellNumber, lambda n: fmul(log(n), 1.2)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Pell number') # Sylvester numbers if n > 1: result = findInput(n, getNthSylvesterNumber, lambda n: sqrt(sqrt(log(n)))) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' Sylvester number') # partition numbers result = findInput(n, getPartitionNumber, lambda n: power(log(n), 1.56)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' partition number') # menage numbers result = findInput(n, getNthMenageNumber, lambda n: fdiv(log10(n), 1.2)) if result[0]: print(indent + 'the ' + getShortOrdinalName(result[1]) + ' menage number') print() print(int(n), 'has:') # number of digits digits = log10(n) if isInteger(digits): digits += 1 else: digits = ceil(digits) print(indent + str(int(digits)) + ' digit' + ('s' if digits > 1 else '')) # digit sum print(indent + 'a digit sum of ' + str(int(sumDigits(n)))) # digit product digitProduct = multiplyDigits(n) print(indent + 'a digit product of ' + str(int(digitProduct))) # non-zero digit product if digitProduct == 0: print(indent + 'a non-zero digit product of ' + str(int(multiplyNonzeroDigits(n)))) if not isPrimeFlag and n != 1 and n <= LARGEST_NUMBER_TO_FACTOR: # factors factors = getFactors(n) factorCount = len(factors) print(indent + str(factorCount) + ' prime factor' + ('s' if factorCount > 1 else '') + ': ' + ', '.join([str(int(i)) for i in factors])) # number of divisors divisorCount = int(getDivisorCount(n)) print(indent + str(divisorCount) + ' divisor' + ('s' if divisorCount > 1 else '')) if n <= LARGEST_NUMBER_TO_FACTOR: print(indent + 'a divisor sum of ' + str(int(getSigma(n)))) print(indent + 'a Stern value of ' + str(int(getNthSternNumber(n)))) calkinWilf = getNthCalkinWilf(n) print(indent + 'a Calkin-Wilf value of ' + str(int(calkinWilf[0])) + '/' + str(int(calkinWilf[1]))) print(indent + 'a Mobius value of ' + str(int(getNthMobiusNumber(n)))) print(indent + 'a radical of ' + str(int(getRadical(n)))) print(indent + 'a Euler phi value of ' + str(int(getEulerPhi(n)))) print(indent + 'a digital root of ' + str(int(getDigitalRoot(n)))) if hasUniqueDigits(n): print(indent + 'unique digits') print(indent + 'a multiplicative persistence of ' + str(int(getPersistence(n)))) print(indent + 'an Erdos persistence of ' + str(int(getErdosPersistence(n)))) if n > 9 and isIncreasing(n): if not isDecreasing(n): print(indent + 'increasing digits') elif n > 9 and isDecreasing(n): print(indent + 'decreasing digits') print() return n
from itertools import combinations import matplotlib.pyplot as plt from mpmath import cbrt import sys import OrbitData times = [] masses = None positions = [] for time, bodies, _ in OrbitData.read(sys.stdin): times.append(time) positions.append(map(lambda b: b[1:3], bodies)) masses = map(lambda b: b[0], bodies) scale = min(masses) masses = [12*cbrt(m/scale)**2 for m in masses] print "Read all data" def update_plot(i, scat, pos, mass): scat.set_offsets(pos[i]) scat.set_sizes(mass) return scat minx = min(map(lambda p: min(map(lambda c: c[0], p)), positions)) maxx = max(map(lambda p: max(map(lambda c: c[0], p)), positions)) miny = min(map(lambda p: min(map(lambda c: c[1], p)), positions)) maxy = max(map(lambda p: max(map(lambda c: c[1], p)), positions)) xbuf = (maxx - minx)/10 maxx += xbuf
def inverse_function(y): return cbrt((y - coefficients[-1]) / coefficients[0])
def test_cbrt_imag_dd_mp(): ans = cbrt_imag_dd(2.718281828459045, 1.4456468917292502e-16, 3.141592653589793, 1.2246467991473532e-16) mp_ans = mp.cbrt(mp.e + mp.pi * 1j) assert abs(mp_ans.real - (mp.mpf(ans[0]) + mp.mpf(ans[1]))) < 1e-30 assert abs(mp_ans.imag - (mp.mpf(ans[2]) + mp.mpf(ans[3]))) < 1e-30
if x == 0: return 1000 return abs(2 * ap.sqrt(x) - x**2 / 2) def approximate(errfunc, startsize=1, startnum=0): # peice of shit is broken estim = ap.mpf(startnum) while error(estim) != 0 and startnum > -ap.mp.dps: value = ap.mpf(10)**startsize for d in range(10): print((d + 1) * value) print(f"value {estim} vs {estim + value}") print(f"errone is {error(estim)}") print(f"errtwo is {error(estim + value)}") if error(estim + value) > error(estim): print("break") input() break input() estim += value startsize -= 1 return estim #print(approximate(error, 0, 2.5)) # f**k this # (2 * sqrt(x)) - (x ^ 2 / 2) = 0 # from wolfram alpha: 2 * cuberoot(2) print(str(2 * ap.cbrt(2)))
while ndigits >= 0: magnitude = base**ndigits d = n // magnitude n = n % magnitude nstring += digitmap[d] ndigits -= 1 return nstring def is_palindrome(n): nstr = str(n) while len(nstr) > 1: if nstr[0] != nstr[-1]: return False nstr = nstr[1:-1] return True i = 0 while True: if mpmath.cbrt(i**2) % 1 == 0: base = to_base(i) if is_palindrome(base): print(f"number is {i}") print(f"{i}^2 ({i**2}) is cube of {int(mpmath.cbrt(i**2))}") print(f"number in base 63 is {base}") input() i += 1
def getPlasticConstant( ): '''Computes and returns the Plastic constant.''' term = fmul( 12, sqrt( 69 ) ) return fdiv( fadd( cbrt( fadd( 108, term ) ), cbrt( fsub( 108, term ) ) ), 6 )
def describeInteger( n ): if n < 1: raise ValueError( "'describe' requires a positive integer argument" ) indent = ' ' * 4 print( ) print( real_int( n ), 'is:' ) if isOdd( n ): print( indent + 'odd' ) elif isEven( n ): print( indent + 'even' ) if isPrimeNumber( n ): isPrime = True print( indent + 'prime' ) elif n > 3: isPrime = False print( indent + 'composite' ) else: isPrime = False if isKthPower( n, 2 ): print( indent + 'the ' + getShortOrdinalName( sqrt( n ) ) + ' square number' ) if isKthPower( n, 3 ): print( indent + 'the ' + getShortOrdinalName( cbrt( n ) ) + ' cube number' ) for i in arange( 4, fadd( ceil( log( fabs( n ), 2 ) ), 1 ) ): if isKthPower( n, i ): print( indent + 'the ' + getShortOrdinalName( root( n, i ) ) + ' ' + \ getNumberName( i, True ) + ' power' ) # triangular guess = findPolygonalNumber( n, 3 ) if getNthPolygonalNumber( guess, 3 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' triangular number' ) # pentagonal guess = findPolygonalNumber( n, 5 ) if getNthPolygonalNumber( guess, 5 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' pentagonal number' ) # hexagonal guess = findPolygonalNumber( n, 6 ) if getNthPolygonalNumber( guess, 6 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' hexagonal number' ) # heptagonal guess = findPolygonalNumber( n, 7 ) if getNthPolygonalNumber( guess, 7 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' heptagonal number' ) # octagonal guess = findPolygonalNumber( n, 8 ) if getNthPolygonalNumber( guess, 8 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' octagonal number' ) # nonagonal guess = findPolygonalNumber( n, 9 ) if getNthPolygonalNumber( guess, 9 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' nonagonal number' ) # decagonal guess = findPolygonalNumber( n, 10 ) if getNthPolygonalNumber( guess, 10 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' decagonal number' ) #if n > 1: # for i in range( 11, 101 ): # if getNthPolygonalNumber( findPolygonalNumber( n, i ), i ) == n: # print( indent + str( i ) + '-gonal' ) # centered triangular guess = findCenteredPolygonalNumber( n, 3 ) if getNthCenteredPolygonalNumber( guess, 3 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' centered triangular' ) # centered square guess = findCenteredPolygonalNumber( n, 4 ) if getNthCenteredPolygonalNumber( guess, 4 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' centered square number' ) # centered pentagonal guess = findCenteredPolygonalNumber( n, 5 ) if getNthCenteredPolygonalNumber( guess, 5 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' centered pentagonal number' ) # centered hexagonal guess = findCenteredPolygonalNumber( n, 6 ) if getNthCenteredPolygonalNumber( guess, 6 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' centered hexagonal number' ) # centered heptagonal guess = findCenteredPolygonalNumber( n, 7 ) if getNthCenteredPolygonalNumber( guess, 7 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' centered heptagonal number' ) # centered octagonal guess = findCenteredPolygonalNumber( n, 8 ) if getNthCenteredPolygonalNumber( guess, 8 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' centered octagonal number' ) # centered nonagonal guess = findCenteredPolygonalNumber( n, 9 ) if getNthCenteredPolygonalNumber( guess, 9 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' centered nonagonal number' ) # centered decagonal guess - findCenteredPolygonalNumber( n, 10 ) if getNthCenteredPolygonalNumber( guess, 10 ) == n: print( indent + 'the ' + getShortOrdinalName( guess ) + ' centered decagonal number' ) # pandigital if isPandigital( n ): print( indent + 'pandigital' ) #for i in range( 4, 21 ): # if isBaseKPandigital( n, i ): # print( indent + 'base ' + str( i ) + ' pandigital' ) # Fibonacci result = findInput( n, fib, lambda n: fmul( log10( n ), 5 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Fibonacci number' ) # Tribonacci result = findInput( n, lambda n : getNthKFibonacciNumber( n, 3 ), lambda n: fmul( log10( n ), 5 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Tribonacci number' ) # Tetranacci result = findInput( n, lambda n : getNthKFibonacciNumber( n, 4 ), lambda n: fmul( log10( n ), 5 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Tetranacci number' ) # Pentanacci result = findInput( n, lambda n : getNthKFibonacciNumber( n, 5 ), lambda n: fmul( log10( n ), 5 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Pentanacci number' ) # Hexanacci result = findInput( n, lambda n : getNthKFibonacciNumber( n, 6 ), lambda n: fmul( log10( n ), 5 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Hexanacci number' ) # Heptanacci result = findInput( n, lambda n : getNthKFibonacciNumber( n, 7 ), lambda n: fmul( log10( n ), 5 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Heptanacci number' ) # Octanacci result = findInput( n, lambda n : getNthKFibonacciNumber( n, 8 ), lambda n: fmul( log10( n ), 5 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Octanacci number' ) # Lucas numbers result = findInput( n, getNthLucasNumber, lambda n: fmul( log10( n ), 5 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Lucas number' ) # base-k repunits if n > 1: for i in range( 2, 21 ): result = findInput( n, lambda x: getNthBaseKRepunit( x, i ), lambda n: log( n, i ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' base-' + str( i ) + ' repunit' ) # Jacobsthal numbers result = findInput( n, getNthJacobsthalNumber, lambda n: fmul( log( n ), 1.6 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Jacobsthal number' ) # Padovan numbers result = findInput( n, getNthPadovanNumber, lambda n: fmul( log10( n ), 9 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Padovan number' ) # Fibonorial numbers result = findInput( n, getNthFibonorial, lambda n: sqrt( fmul( log( n ), 10 ) ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Fibonorial number' ) # Mersenne primes result = findInput( n, getNthMersennePrime, lambda n: fadd( fmul( log( log( sqrt( n ) ) ), 2.7 ), 3 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Mersenne prime' ) ## perfect number result = findInput( n, getNthPerfectNumber, lambda n: fmul( log( log( n ) ), 2.6 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' perfect number' ) # Mersenne exponent result = findInput( n, getNthMersenneExponent, lambda n: fmul( log( n ), 2.7 ), max=50 ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Mersenne exponent' ) if not isPrime and n != 1 and n <= largestNumberToFactor: # deficient if isDeficient( n ): print( indent + 'deficient' ) # abundant if isAbundant( n ): print( indent + 'abundant' ) # k_hyperperfect for i in sorted( list( set( sorted( downloadOEISSequence( 34898 )[ : 500 ] ) ) ) )[ 1 : ]: if i > n: break if isKHyperperfect( n, i ): print( indent + str( i ) + '-hyperperfect' ) break # smooth for i in getPrimes( 2, 50 ): if isSmooth( n, i ): print( indent + str( i ) + '-smooth' ) break # rough previousPrime = 2 for i in getPrimes( 2, 50 ): if not isRough( n, i ): print( indent + str( previousPrime ) + '-rough' ) break previousPrime = i # is_semiprime if isSemiprime( n ): print( indent + 'semiprime' ) # is_sphenic elif isSphenic( n ): print( indent + 'sphenic' ) elif isSquareFree( n ): print( indent + 'square-free' ) # powerful if isPowerful( n ): print( indent + 'powerful' ) # factorial result = findInput( n, getNthFactorial, lambda n: power( log10( n ), 0.92 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' factorial number' ) # alternating factorial result = findInput( n, getNthAlternatingFactorial, lambda n: fmul( sqrt( log( n ) ), 0.72 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' alternating factorial number' ) # double factorial if n == 1: result = ( True, 1 ) else: result = findInput( n, getNthDoubleFactorial, lambda n: fdiv( power( log( log( n ) ), 4 ), 7 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' double factorial number' ) # hyperfactorial result = findInput( n, getNthHyperfactorial, lambda n: fmul( sqrt( log( n ) ), 0.8 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' hyperfactorial number' ) # subfactorial result = findInput( n, getNthSubfactorial, lambda n: fmul( log10( n ), 1.1 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' subfactorial number' ) # superfactorial if n == 1: result = ( True, 1 ) else: result = findInput( n, getNthSuperfactorial, lambda n: fadd( sqrt( log( n ) ), 1 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' superfactorial number' ) # pernicious if isPernicious( n ): print( indent + 'pernicious' ) # pronic if isPronic( n ): print( indent + 'pronic' ) # Achilles if isAchillesNumber( n ): print( indent + 'an Achilles number' ) # antiharmonic if isAntiharmonic( n ): print( indent + 'antiharmonic' ) # unusual if isUnusual( n ): print( indent + 'unusual' ) # hyperperfect for i in range( 2, 21 ): if isKHyperperfect( n, i ): print( indent + str( i ) + '-hyperperfect' ) # Ruth-Aaron if isRuthAaronNumber( n ): print( indent + 'a Ruth-Aaron number' ) # Smith numbers if isSmithNumber( n ): print( indent + 'a Smith number' ) # base-k Smith numbers for i in range( 2, 10 ): if isBaseKSmithNumber( n, i ): print( indent + 'a base-' + str( i ) + ' Smith number' ) # order-k Smith numbers for i in range( 2, 11 ): if isOrderKSmithNumber( n, i ): print( indent + 'an order-' + str( i ) + ' Smith number' ) # polydivisible if isPolydivisible( n ): print( indent + 'polydivisible' ) # Carol numbers result = findInput( n, getNthCarolNumber, lambda n: fmul( log10( n ), fdiv( 5, 3 ) ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Carol number' ) # Kynea numbers result = findInput( n, getNthKyneaNumber, lambda n: fmul( log10( n ), fdiv( 5, 3 ) ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Kynea number' ) # Leonardo numbers result = findInput( n, getNthLeonardoNumber, lambda n: fsub( log( n, phi ), fdiv( 1, phi ) ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Leonardo number' ) # Riesel numbers result = findInput( n, getNthRieselNumber, lambda n: fmul( log( n ), 1.25 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Riesel number' ) # Thabit numbers result = findInput( n, getNthThabitNumber, lambda n: fmul( log10( n ), 3.25 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Thabit number' ) # Carmmichael if isCarmichaelNumber( n ): print( indent + 'a Carmichael number' ) # narcissistic if isNarcissistic( n ): print( indent + 'narcissistic' ) # PDI if isPerfectDigitalInvariant( n ): print( indent + 'a perfect digital invariant' ) # PDDI if isPerfectDigitToDigitInvariant( n, 10 ): print( indent + 'a perfect digit-to-digit invariant in base 10' ) # Kaprekar if isKaprekar( n ): print( indent + 'a Kaprekar number' ) # automorphic if isAutomorphic( n ): print( indent + 'automorphic' ) # trimorphic if isTrimorphic( n ): print( indent + 'trimorphic' ) # k-morphic for i in range( 4, 11 ): if isKMorphic( n, i ): print( indent + str( i ) + '-morphic' ) # bouncy if isBouncy( n ): print( indent + 'bouncy' ) # step number if isStepNumber( n ): print( indent + 'a step number' ) # Apery numbers result = findInput( n, getNthAperyNumber, lambda n: fadd( fdiv( log( n ), 1.5 ), 1 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Apery number' ) # Delannoy numbers result = findInput( n, getNthDelannoyNumber, lambda n: fmul( log10( n ), 1.35 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Delannoy number' ) # Schroeder numbers result = findInput( n, getNthSchroederNumber, lambda n: fmul( log10( n ), 1.6 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Schroeder number' ) # Schroeder-Hipparchus numbers result = findInput( n, getNthSchroederHipparchusNumber, lambda n: fdiv( log10( n ), 1.5 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Schroeder-Hipparchus number' ) # Motzkin numbers result = findInput( n, getNthMotzkinNumber, lambda n: log( n ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Motzkin number' ) # Pell numbers result = findInput( n, getNthPellNumber, lambda n: fmul( log( n ), 1.2 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Pell number' ) # Sylvester numbers if n > 1: result = findInput( n, getNthSylvesterNumber, lambda n: sqrt( sqrt( log( n ) ) ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' Sylvester number' ) # partition numbers result = findInput( n, getPartitionNumber, lambda n: power( log( n ), 1.56 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' partition number' ) # menage numbers result = findInput( n, getNthMenageNumber, lambda n: fdiv( log10( n ), 1.2 ) ) if result[ 0 ]: print( indent + 'the ' + getShortOrdinalName( result[ 1 ] ) + ' menage number' ) print( ) print( int( n ), 'has:' ) # number of digits digits = log10( n ) if isInteger( digits ): digits += 1 else: digits = ceil( digits ) print( indent + str( int( digits ) ) + ' digit' + ( 's' if digits > 1 else '' ) ) # digit sum print( indent + 'a digit sum of ' + str( int( sumDigits( n ) ) ) ) # digit product digitProduct = multiplyDigits( n ) print( indent + 'a digit product of ' + str( int( digitProduct ) ) ) # non-zero digit product if digitProduct == 0: print( indent + 'a non-zero digit product of ' + str( int( multiplyNonzeroDigits( n ) ) ) ) if not isPrime and n != 1 and n <= largestNumberToFactor: # factors factors = getFactors( n ) factorCount = len( factors ) print( indent + str( factorCount ) + ' prime factor' + ( 's' if factorCount > 1 else '' ) + \ ': ' + ', '.join( [ str( int( i ) ) for i in factors ] ) ) # number of divisors divisorCount = int( getDivisorCount( n ) ) print( indent + str( divisorCount ) + ' divisor' + ( 's' if divisorCount > 1 else '' ) ) if n <= largestNumberToFactor: print( indent + 'a sum of divisors of ' + str( int( getSigma( n ) ) ) ) print( indent + 'a Stern value of ' + str( int( getNthStern( n ) ) ) ) calkin_wilf = getNthCalkinWilf( n ) print( indent + 'a Calkin-Wilf value of ' + str( int( calkin_wilf[ 0 ] ) ) + '/' + str( int( calkin_wilf[ 1 ] ) ) ) print( indent + 'a Mobius value of ' + str( int( getMobius( n ) ) ) ) print( indent + 'a radical of ' + str( int( getRadical( n ) ) ) ) print( indent + 'a Euler phi value of ' + str( int( getEulerPhi( n ) ) ) ) print( indent + 'a digital root of ' + str( int( getDigitalRoot( n ) ) ) ) if hasUniqueDigits( n ): print( indent + 'unique digits' ) print( indent + 'a multiplicative persistence of ' + str( int( getPersistence( n ) ) ) ) print( indent + 'an Erdos persistence of ' + str( int( getErdosPersistence( n ) ) ) ) if n > 9 and isIncreasing( n ): if not isDecreasing( n ): print( indent + 'increasing digits' ) elif n > 9 and isDecreasing( n ): print( indent + 'decreasing digits' ) print( ) return n
'imag': ['primitive', [lambda x, y: x.imag, None]], 'pi': mp.pi, 'degree': mp.degree, #pi / 180 'e': mp.e, 'phi': mp.phi, #golden ratio = 1.61803... 'euler': mp.euler, #euler's constance = 0.577216... 'mpf': ['primitive', [lambda x, y: mp.mpf(str(x)), None]], 'mpc': ['primitive', [lambda x, y: mp.mpc(x, y[0]), None]], # 'sqrt': ['primitive', [lambda x, y: mp.sqrt(x), None]], 'cbrt': ['primitive', [lambda x, y: mp.cbrt(x), None]], 'root': ['primitive', [lambda x, y: mp.root(x, y[0]), None]], # y's root 'unitroots': ['primitive', [lambda x, y: Vector(mp.unitroots(x)), None]], # 'hypot': ['primitive', [lambda x, y: mp.hypot(x, y[0]), None]], # sqrt(x**2+y**2) # 'sin': ['primitive', [lambda x, y: mp.sin(x), None]], 'cos': ['primitive', [lambda x, y: mp.cos(x), None]], 'tan': ['primitive', [lambda x, y: mp.tan(x), None]], 'sinpi': ['primitive', [lambda x, y: mp.sinpi(x), None]], #sin(x * pi) 'cospi': ['primitive', [lambda x, y: mp.cospi(x), None]], 'sec': ['primitive', [lambda x, y: mp.sec(x), None]], 'csc': ['primitive', [lambda x, y: mp.csc(x), None]], 'cot': ['primitive', [lambda x, y: mp.cot(x), None]], 'asin': ['primitive', [lambda x, y: mp.asin(x), None]],