Hi again!
Some time spent on the internet departing from the wikipedia page on number sequences gave these number sequence functions. Mostly number sequences with some graphical interpretation.
Some time spent on the internet departing from the wikipedia page on number sequences gave these number sequence functions. Mostly number sequences with some graphical interpretation.
from itertools import groupby import math # Some integer sequences that have at least one graphical representation # In all these functions "seq" is the generated sequence def fibn(start, length, n): '''Function to generate the n-th number sequence generalization of the Fibonacci number sequence. start == 1 and n == 2 => Fibonacci number sequence n == 3 => "Tribonacci" number sequence n == 4 => "Tetranacci" number sequence and so on ... start == 2 and n == ... (test it) A generalization of your gen_fib function ''' if n < 2: return [] seq = [0]*(n-1)+[start] b = 0 for j in range(length): seq.append(sum(seq[b:])) b += 1 return seq[0:length+1] def lucas(n): # https://en.wikipedia.org/wiki/Lucas_number seq = [2, 1] while len(seq) < n: seq.append(seq[-1]+seq[-2]) return seq[:n] def partitions_count(n): ''' Help function for partitions, found on stackoverflow.com Gives the number of ways of writing the integer n as a sum of positive integers, where the order of addends is not considered significant. ''' dic = {} def p(n, k): '''Gives the number of ways of writing n as a sum of exactly k terms or, equivalently, the number of partitions into parts of which the largest is exactly k. ''' if n < k: return 0 if n == k: return 1 if k == 0: return 0 key = str(n) + ',' + str(k) try: temp = dic[key] except: temp = p(n-1, k-1) + p(n-k, k) dic[key] = temp finally: return temp partitions_count = 0 for k in range(n + 1): partitions_count += p(n, k) return partitions_count def partitions(n): # https://en.wikipedia.org/wiki/Partition_number # The help function partitions_count was found on stackoverflow.com ''' seq = [] for i in range(n): seq.append(partitions_count(i)) return seq def catalan(n): # https://en.wikipedia.org/wiki/Catalan_number seq = [] for i in range(n+1): seq.append(math.comb(2*i, i) - math.comb(2*i, i+1)) return seq def lazy_caterers(n): # https://en.wikipedia.org/wiki/Lazy_caterer%27s_sequence seq = [] for i in range(n+1): seq.append((i*i + i + 2) // 2) return seq def pell(n): # https://en.wikipedia.org/wiki/Pell_number seq = [0, 1] while len(seq) < n: seq.append(2*seq[-1] + seq[-2]) return seq[0:n] def padovan(n): # https://en.wikipedia.org/wiki/Padovan_sequence seq = [1, 1, 1] while len(seq) < n+1: seq.append(seq[-2]+seq[-3]) return seq[0:n+1] def lucky_numbers(n): # https://en.wikipedia.org/wiki/Lucky_number seq = range(-1,n*n+9,2) i = 2 while seq[i:]: seq=sorted(set(seq)-set(seq[seq[i]::seq[i]])) i += 1 return seq[1:n+1] def motzkin(n): # https://en.wikipedia.org/wiki/Motzkin_number seq = [0]*(n+1) seq[0] = 1 for i in range(1, n+1): seq[i] = seq[i-1] for j in range(i-1): seq[i] = seq[i] + seq[j] * seq[i-j-2] return seq def wedderburn_etherington(n): # https://en.wikipedia.org/wiki/Wedderburn%E2%80%93Etherington_number # Found this on GeeksForGeeks, twisted it to generate a list store = dict() def w_e(n): if (n <= 2): return store[n] if (n % 2 == 0): x = n // 2 ans = 0 for i in range(1, x): ans += store[i] * store[n - i] ans += (store[x] * (store[x] + 1)) // 2 store[n] = ans return ans else: x = (n + 1) // 2 ans = 0 for i in range(1, x): ans += store[i] * store[n - i] store[n] = ans return ans store[0] = 0 store[1] = 1 store[2] = 1 for i in range(n): w_e(i) return list(store.values())[0:n] def perrin(n): # https://en.wikipedia.org/wiki/Perrin_number seq = [3, 0, 2] while len(seq) < n + 1: seq.append(seq[-2]+seq[-3]) return seq[0:n+1] def pronic(n): # https://en.wikipedia.org/wiki/Pronic_number i = 1 seq = [] while len(seq) < n: seq.append(i * (i + 1)) i += 1 return seq def look_and_say(n): # https://en.wikipedia.org/wiki/Look-and-say_sequence seq = ['1'] for i in range(n): seq.append(''.join(str(len(list(group))) + key for key, group in groupby(seq[-1]))) return seqAll tested and working! More will come...