comma-series.html
001| '''The Comma Sequence.
001|
001| The "comma sequence" starts with 1 and is defined by the property that if
001| k and k' are consecutive terms, the two-digit number formed from the last
001| digit of k and the first digit of k' is equal to the difference k'-k.
001|
001| If there is more than one such k', choose the smallest, but if there is
001| no such k' the sequence terminates.
001|
001| Eric Angelini, et al.
001| https://arxiv.org/abs/2401.14346
001| '''
002|
003| from sys import argv
004| from os import path
005| from itertools import count
006| from files import linefile
007|
008| BasePath = r'C:\CJS\prj\Python\blog\proj'
009| DataPath = path.join(BasePath, 'dat')
010|
011|
012| demo = (1, 12, 35, 94, 135, 186, 248, 331, 344, 387, 461, 475, 530, 535, 590, 595, 651, 667, 744,
013| 791, 809, 908, 997, 1068, 1149, 1240, 1241, 1252, 1273, 1304, 1345, 1396, 1457, 1528, 1609,
014| 1700, 1701, 1712, 1733, 1764, 1805, 1856, 1917, 1988, 2070, 2072, 2094, 2136, 2198, 2280)
015|
016|
017|
018| def test_sequence (seq):
019| '''Test SEQ as a Comma Sequence.'''
020| print(seq)
021| print()
022| css = [str(n) for n in seq]
023|
024| for ix,a,b in zip(count(1),css,css[1:]):
025| dif = int(f'{a[-1]}{b[0]}')
026| assert (int(a) + dif) == int(b), ValueError(f'Incorrect sequence values! {a},{b}')
027| print(f'{ix:04d}: {a} + {dif:02d} = {b}')
028| print()
029|
030|
031| def find_next_number (n, log):
032| '''Find next number in Comma Sequence given N.'''
033|
034| last_digit = str(n)[-1]
035| log.add(f'{n=}')
036| log.add(f'{last_digit=}')
037|
038| jumps = [int(f'{last_digit}{d}') for d in range(1,10)]
039| targs = [n+p for p in jumps]
040| spans = [int(f'{last_digit}{str(t)[0]}') for t in targs]
041| check = [j-s for j,s in zip(jumps,spans)]
042|
043| log.add(f'{jumps=}')
044| log.add(f'{spans=}')
045| log.add(f'{check=}')
046| log.add(f'{targs=}')
047|
048|
049| if 0 in check:
050| ix = check.index(0)
051| plus = jumps[ix]
052| targ = targs[ix]
053| log.add(f'found: {n} + {plus:02d} = {targ}')
054| log.add()
055| return (plus, targ)
056|
057| log.add(f'nadda: {n} {check}')
058| log.add()
059| return (0,n)
060|
061|
062| def generate_sequence (seq_length, start=1, fname='comma-series.log', fpath=DataPath):
063| '''Generate a Comma Sequence COUNT long.'''
064| fn = path.join(fpath, fname) if fpath else fname
065| fo = linefile(fn)
066|
067| nxt = start
068| seq = [nxt]
069| for n in range(seq_length):
070| add,nxt = find_next_number(nxt, fo)
071| if add == 0:
072| print(f'Sequence breaks at {nxt}')
073| print()
074| break
075| seq.append(nxt)
076|
077| fo.add()
078| fo.save()
079| print(f'wrote: {fn}')
080| print()
081| return seq
082|
083|
084| def main (*args):
085| seq_len = int(args[0]) if 0 < len(args) else 25
086| start = int(args[1]) if 1 < len(args) else 1
087|
088| seq = generate_sequence(seq_len, start=start)
089| test_sequence(seq)
090| return seq
091|
092|
093| def dispatch (cmd, *args):
094| print(f'{cmd=}')
095| print()
096|
097| if cmd == 'test': return test_sequence(demo)
098| if cmd == 'main': return main(*args)
099| return cmd
100|
101| if __name__ == '__main__':
102| print(argv[0])
103| print()
104| cmnd = argv[1] if 1 < len(argv) else 'main'
105| args = argv[2:] if 2 < len(argv) else ['10000']
106| rv = dispatch(cmnd, *args)
107| print()
108|
109| '''eof'''
110|