Python Forum
[PyGame] Fibonacci graphics ideas?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Fibonacci graphics ideas?
#13
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.
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 seq
All tested and working! More will come...
Reply


Messages In This Thread
Fibonacci graphics ideas? - by michael1789 - Mar-03-2021, 04:15 AM
RE: Fibonacci graphics ideas? - by metulburr - Mar-04-2021, 02:15 AM
RE: Fibonacci graphics ideas? - by BashBedlam - Mar-04-2021, 03:09 AM
RE: Fibonacci graphics ideas? - by michael1789 - Mar-04-2021, 05:48 PM
RE: Fibonacci graphics ideas? - by michael1789 - Mar-04-2021, 03:34 AM
RE: Fibonacci graphics ideas? - by Serafim - Mar-04-2021, 09:47 AM
RE: Fibonacci graphics ideas? - by michael1789 - Mar-04-2021, 04:03 PM
RE: Fibonacci graphics ideas? - by Serafim - Mar-04-2021, 06:29 PM
RE: Fibonacci graphics ideas? - by michael1789 - Mar-04-2021, 06:09 PM
RE: Fibonacci graphics ideas? - by michael1789 - Mar-06-2021, 12:05 AM
RE: Fibonacci graphics ideas? - by Serafim - Mar-06-2021, 12:44 PM
RE: Fibonacci graphics ideas? - by michael1789 - Mar-06-2021, 06:26 PM
RE: Fibonacci graphics ideas? - by Serafim - Mar-08-2021, 08:21 PM
RE: Fibonacci graphics ideas? - by Serafim - Mar-08-2021, 08:55 PM
RE: Fibonacci graphics ideas? - by Serafim - Mar-10-2021, 10:36 AM
RE: Fibonacci graphics ideas? - by Serafim - Mar-10-2021, 09:19 PM
RE: Fibonacci graphics ideas? - by michael1789 - Mar-11-2021, 03:27 AM
RE: Fibonacci graphics ideas? - by Serafim - Mar-11-2021, 08:32 AM
RE: Fibonacci graphics ideas? - by Serafim - Mar-12-2021, 09:55 AM
RE: Fibonacci graphics ideas? - by Serafim - Mar-12-2021, 03:55 PM
RE: Fibonacci graphics ideas? - by michael1789 - Mar-12-2021, 04:40 PM
RE: Fibonacci graphics ideas? - by Serafim - Mar-14-2021, 08:57 AM
RE: Fibonacci graphics ideas? - by michael1789 - Mar-15-2021, 03:31 PM
RE: Fibonacci graphics ideas? - by Serafim - Mar-16-2021, 09:10 AM
RE: Fibonacci graphics ideas? - by Serafim - Mar-17-2021, 03:04 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020