Hickerson series of almost integers

From Rosetta Code
Revision as of 10:52, 1 January 2014 by rosettacode>Paddy3118 (New draft task and Python solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Hickerson series of almost integers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The following function, due to D Hickerson is said to generate "Almost integers" by the "Almost Integer" page of Wolfram Mathworld. (December 31 2013).

The function is:

   h(n) = n! / ( 2 * ln(n)^(n + 1) )
   for 1 <= n <= 17

The function is said to produce "almost integers" for n in the given range.

Assume that an "almost integer" has either a nine or a zero as its first digit after the decimal point of its decimal string representation

The task is to calculate all values of the function checking and stating which are "almost integers".

Note: Use extended/arbitrary precision numbers in your calculation if necessary to ensure you have adequate precision of results as for example:

   h(18) = 3385534663256845326.39...

Python

This uses Pythons decimal module of fixed precision decimal floating point calculations.

<lang python>from decimal import Decimal import math

def h(n):

   'Simple, reduced precision calculation'
   return math.factorial(n) / (2 * math.log(2) ** (n + 1))
   

def h2(n):

   'Extended precision Hickerson function'
   return Decimal(math.factorial(n)) / (2 * Decimal(2).ln() ** (n + 1))

for n in range(18):

   x = h2(n)
   norm = str(x.normalize())
   almostinteger = (' Nearly integer' 
                    if 'E' not in norm and ('.0' in norm or '.9' in norm) 
                    else ' NOT nearly integer!')
   print('n:%2i h:%s%s' % (n, norm, almostinteger))</lang>
Output:
n: 0 h:0.7213475204444817036799623405 NOT nearly integer!
n: 1 h:1.040684490502803898934790802 Nearly integer
n: 2 h:3.002780707156905443499767406 Nearly integer
n: 3 h:12.99629050527696646222488454 Nearly integer
n: 4 h:74.99873544766160012763455035 Nearly integer
n: 5 h:541.0015185164235075692027746 Nearly integer
n: 6 h:4683.001247262257437180467151 Nearly integer
n: 7 h:47292.99873131462390482283547 Nearly integer
n: 8 h:545834.9979074851670672910395 Nearly integer
n: 9 h:7087261.001622899120979187513 Nearly integer
n:10 h:102247563.0052710420110883885 Nearly integer
n:11 h:1622632572.997550049852874859 Nearly integer
n:12 h:28091567594.98157244071518915 Nearly integer
n:13 h:526858348381.0012482861804887 Nearly integer
n:14 h:10641342970443.08453192709506 Nearly integer
n:15 h:230283190977853.0374360391257 Nearly integer
n:16 h:5315654681981354.513076743451 NOT nearly integer!
n:17 h:130370767029135900.4579853491 NOT nearly integer!

The range for should be reduced to be 1<= n <= 15 for this definition of almost integer.