primes_number.html


001| '''Number Sequence to Decimal Number.
001| 
001| Convert an increasing sequence of positive integers to a decimal number.
001| 
001| Treat each member of the sequence as an index to a bit in a binary fraction.
001| We ignore zero. A value of 1 indexes the first binary fraction bit (1/b^1=0.5),
001| a value of 2 indexes the second (1/b^2=0.25), and so on. The largest number
001| (which should also be the last number) in the sequence determines the highest
001| index. For instance, if highest value is 1021, this sets bit b^(1021).
001| 
001| The decimal number for the prime numbers (not including 1) up to 1,021:
001| 
001| 0.414682509851111660248109622154301545389952198983
001| 18719873882914272122163869408304495406528062432996
001| 49513349093520829859578357499784530220099041208146
001| 50033958993701974119186285615579237191637251488161
001| 07107342843248021801697985681513424679474924390277
001| 88203115377831806629878700354471810730031528229365
001| 86571701195829852701391794757581454267068859883684
001| 50057419679090970590126513469359487131748820023386
001| 51383968278639060580547694400606775567007462220308
001| 21840472589398024405181338703125633822847018657517
001| 94441811587128454329522065692395520562236572812322
001| 30948558722153467802070765239806635595635256623658
001| 09776495698183599177593072910748736171664601001058
001| 79824716697608725256969266948043624265510710754181
001| 72042158356715710211009002446239549475692248093519
001| 58969021686450524779004531373956569193038239101489
001| 32777864564550019249469266544218620641859060020931
001| 79166469799727236169453540031938273217425197819335
001| 94774292921893724306981953242902121152927011496755
001| 35377973005563859500071535957527527060939576131204
001| 37599718570709228515625 (1021 decimal digits)
001| 
001| The decimal number for the Fibonacci numbers up to 1,597:
001| 
001| 0.910278797207865891794043024471063144483423924595
001| 27877259329246793007351682602794535161233012145902
001| 33285193299335674796261965282643469378233326368385
001| 00852148379233090009251388830700905431279287214949
001| 99481835421441490412975947676826679087446287105998
001| 65939980600226924511009325820625404889174821010268
001| 16801341986110527693145265408497099679110255352398
001| 89544522559790002242379430381724611377483384009697
001| 17889928184152001619786399998006623595052763803360
001| 87235865509212455182460141676175740388585826055979
001| 72154086940782466860687193027229790086767728077703
001| 77902449751243680122728777247176894222385892651729
001| 08771980327622077341319616842755597169465295706647
001| 30304510371314704191847832616685937898055474745105
001| 35862431825455047647170860808649671140482679032895
001| 95587817258214731466384053557922190058423560704030
001| 54438958181434371413969078080954369444591621839479
001| 81446212703285534167421591282395101431297442121764
001| 48925995354508738254179187202525299173010898485598
001| 07162689594413628403716361652681563750404744002122
001| 97839032841946319253047719634903690587531692777370
001| 28207503125468566699416247704355697890468582492383
001| 41152193328102457171623828013598158699997907115570
001| 17555808154186632130544668977599687763218433264212
001| 16393494320142122944008504994361763106339976687205
001| 20429586115151825229189688309412614064781587961969
001| 02784777863809453107942642745558636983129791596632
001| 15038492088053240820599673913407412921965519762958
001| 44477719472330320616070603835168093418007224247575
001| 34620656469770112567514581181770486048409537026881
001| 69461387773673810419593057125441063843322588377676
001| 0527514755605427154705466818995773792266845703125
001| 
001| '''

002| ####################################################################################################
003| from sys import argv
004| from n256 import bfracs, digbuf
005| ####################################################################################################
006| 
007| ##------------------------------------------------------------------------------------------------##
008| primes = [
009|     2,3,5,7, 11,13, 17,19, 23, 29,31, 37, 41,43, 47, 53, 59,61, 67, 71,73, 79, 83, 89, 97,
010|     101,103, 109, 113, 127, 131, 137,139, 149,151, 157, 163, 167, 173, 179,181, 191,193, 197,199,
011|     211, 223, 227,229, 233, 239,241, 251, 257, 263, 269,271, 277, 281,283, 293,
012|     307, 311,313, 317, 331, 337, 347,349, 353, 359, 367, 373, 379, 383, 389, 397,
013|     401, 409, 419,421, 431,433, 439, 443, 449, 457, 461,463, 467, 479, 487, 491, 499,
014|     503, 509, 521,523, 541, 547, 557, 563, 569,571, 577, 587, 593, 599,
015|     601, 607, 613, 617,619, 631, 641,643, 647, 653, 659, 661, 673, 677, 683, 691,
016|     701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797,
017|     809,811, 821,823, 827,829, 839, 853, 857,859, 863, 877, 881,883, 887,
018|     907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997,
019|     1009, 1013, 1019, 1021
020| ]
021| 
022| ##------------------------------------------------------------------------------------------------##
023| fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]
024| 
025| ##------------------------------------------------------------------------------------------------##
026| sqrs = [pow(n,2) for n in range(16)]
027| 
028| 
029| ##================================================================================================##
030| def old_generate_number_from_sequence (number_sequence, precision=100, escape=70, verbose=False):
031|     if precision < max(number_sequence):
032|         raise ValueError(f'Max bit index is {max(number_sequence)} but precision is only {precision}.')
033| 
034|     bitmask = [0]*precision
035|     for n in number_sequence:
036|         if 0 < n:
037|             bitmask[n-1] = 1
038| 
039|     number = 0.0
040|     factor = 0.5
041|     for bx,b in enumerate(bitmask):
042|         if verbose:
043|             print(f'{b} => {number:.24f} {factor:.24f}')
044|         if b:
045|             number += factor
046|         factor /= 2.0
047|         if escape < bx:
048|             if verbose:
049|                 print(f'Escaping after {bx} iterations.')
050|             break
051| 
052|     if verbose:
053|         print(*bitmask, sep='')
054|     print(number)
055|     print()
056|     return number
057| 
058| f0 = old_generate_number_from_sequence
059| 
060| ##================================================================================================##
061| def generate_number_from_sequence (number_sequence, precision=2000, verbose=False):
062|     m = 0
063|     for ix,nbr in enumerate(number_sequence, start=1):
064|         if isinstance(nbr,int): continue
065|         if isinstance(nbr,float): continue
066|         raise ValueError(f'Not a number: seq[{ix}]={nbr}')
067|     m = -1
068|     for ix,nbr in enumerate(number_sequence):
069|         if nbr < m:
070|             raise ValueError(f'Sequence decreases at [{ix}]={nbr}')
071|         m = nbr
072|     if precision < m:
073|         raise ValueError(f'Max bit index is {m} but precision is only {precision}.')
074| 
075|     bitmask = [0]*precision
076|     for n in number_sequence:
077|         if 0 < n:
078|             bitmask[n-1] = 1
079| 
080|     bf = bfracs()
081|     bf.load(precision)
082|     dbuf = digbuf([0])
083| 
084|     # Header...
085|     if verbose:
086|         print(''.join(map(str, dbuf)))
087|         print()
088| 
089|     # Generate number...
090|     for bx,b in enumerate(bitmask):
091|         if b:
092|             dadd = bf[bx]
093|             dbuf.add_to(dadd)
094|             if verbose:
095|                 print(''.join(map(str, dadd)))
096|                 print(''.join(map(str, dbuf)))
097|                 print()
098| 
099|     # Trailer...
100|     if verbose:
101|         print()
102| 
103|     # Display number...
104|     print('0.',''.join(map(str, dbuf)),sep='')
105|     print()
106|     return dbuf
107| 
108| f1 = generate_number_from_sequence
109| 
110| 
111| ##================================================================================================##
112| def main ():
113|     small_primes = [n for n in primes if n<100]
114|     small_fibs = [n for n in fibs if n<200]
115| 
116|     # 0.41468250985111166
117|     #rv = f0(small_primes, verbose=True)
118| 
119|     # 0.9102787972078659
120|     #rv = f0(small_fibs, precision=145, verbose=True)
121| 
122|     # 0.5644684136059386
123|     #rv = f0(sqrs, precision=256, verbose=True)
124|     #print()
125| 
126|     # 0.4146825098511116602481096221538068702774843473102611636667280681223246574518270790576934814453125
127|     # 0.41468250985111166024810962215430154538995219898318719873882914272122163869408304495406528...
128|     rv = f1(small_primes, precision=100, verbose=False)
129|     #rv = f1(primes, precision=1024, verbose=False)
130|     rv = f1(small_primes+[101], precision=101, verbose=False)
131|     rv = f1(small_primes+[101,103], precision=103, verbose=False)
132| 
133|     # 0.91027879720786589179404302447106314448342392459527877259329246793007344438059531473791339...
134|     # 0.91027879720786589179404302447106314448342392459527877259329246793007344438059531473791339...
135|     #rv = f1(small_fibs, precision=1600, verbose=False)
136|     #rv = f1(fibs, precision=1600, verbose=False)
137| 
138|     # 0.56446841360593857933472927427247566230625826997043904644450559602848013317956977049057454...
139|     #rv = f1(sqrs, precision=1024, verbose=False)
140| 
141|     return rv
142| 
143| ####################################################################################################
144| if __name__ == '__main__':
145|     print(argv[0])
146|     print()
147|     rv = main()
148|     print()
149| ####################################################################################################
150| '''eof'''
151|