2021-02-08 MATH 3600 Factor Tree Dictionaries

301 days ago by calkin

dict_1= {1:'red',33:4} 
       
dict_1.update({3:'yellow'}) 
       
dict_1 
       
{1: 'red', 3: 'yellow', 33: 4}
{1: 'red', 3: 'yellow', 33: 4}
dict_1.update({'[[]]':[2,3,5,7]}) 
       
dict_1 
       
{1: 'red', 3: 'yellow', 33: 4, '[[]]': [2, 3, 5, 7], '[]': 1}
{1: 'red', 3: 'yellow', 33: 4, '[[]]': [2, 3, 5, 7], '[]': 1}
dict_1['[[]]'].append(11) 
       
dict_1.has_key('[[],[]]') 
       
False
False
 
       
 
       
dict_1['[[]]'] 
       
[2, 3, 5, 7, 11]
[2, 3, 5, 7, 11]

For keeping track of factor trees: we can use dictionaries in different ways:

1.  Factor_Tree is a dictionary with keys 2,3,4,5,6,7..... and Factor_Tree[n] is the factor tree for n.  Note: this doesn't need to be in quotes.

2. First_Occurrence is a dictionary with keys '[[]]', '[[]'[]]',... and the value for a given key is the first occurrence of n with that factor tree shape.

3. All_Occurrences is a dictionary with the same keys, and the values is a list of all n which we've seen which have that shape of factor tree.

def ft(n): if n.is_prime(): return([[n]]) divisor_list = n.divisors() l=len(divisor_list) #print(divisor_list) if is_even(l): a=divisor_list[l/2-1] b=divisor_list[l/2] #print(a,b) return([ft(a),ft(b)]) else: a=divisor_list[(l-1)/2] return([ft(a),ft(a)]) 
       
ft(2) 
       
[[2]]
[[2]]
def ft_unlabeled(n): if n.is_prime(): return([[]]) divisor_list = n.divisors() l=len(divisor_list) #print(divisor_list) if is_even(l): a=divisor_list[l/2-1] b=divisor_list[l/2] #print(a,b) return([ft_unlabeled(a),ft_unlabeled(b)]) else: a=divisor_list[(l-1)/2] return([ft_unlabeled(a),ft_unlabeled(a)]) 
       
ft_unlabeled(8) 
       
[[[]], [[[]], [[]]]]
[[[]], [[[]], [[]]]]
divisor_list = n.divisors() l=len(divisor_list) print l 
       
4
4