Skip to content

Instantly share code, notes, and snippets.

@nathan-sixnines
Last active January 14, 2019 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathan-sixnines/d776d6a0dedca23f4cb281548645df30 to your computer and use it in GitHub Desktop.
Save nathan-sixnines/d776d6a0dedca23f4cb281548645df30 to your computer and use it in GitHub Desktop.
import math
import numpy as np
# very simple discrete "convolution" of 1D filter [1,1,0] over input B
# example: input: 00010
# output: 00110
# input: 01110
# output: 12210
def convoleBasic(a,b):
a += b
a[:-1] += b[1:]
return a
def pascals(rows): # simple way of making pascals triangle without running into floating point presicion problems at larger values
result = []
row = np.array([1], np.uint64)
result.append(np.copy(row))
for count in range(rows-1):
b = np.zeros((row.shape[0]+1), np.uint64)
a = np.zeros((b.shape), np.uint64)
b[1:] = row
a = convoleBasic(a,b)
row = a
result.append(np.copy(row))
return result
rowSums = []
for i, row in enumerate(pascals(130)):
if(np.sum(np.array(row)) != 1 << i): # check for invalid row (row sum != 2^i)
print(np.sum(np.array(row)))
print(1 << i)
print(i)
break # invalid row detected (I think this is an overflow issue), terms before this occurs should be correct
else:
rowSums.append(int(np.sum(np.mod(np.array(row), i+1))))
print(rowSums)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment