Puzzles
Games
Sequences
Arrays
Data Types
Arithmetic
Mathematics
Algorithms
Logic
Numbers
Recursion
Computability Theory
Theoretical Computer Science
def ulam_sequence(u = 1, v = 2, n = 10): print() try: # Input manipulation just for fun :) if u == v or any(map(lambda x: int(x) != x or x < 1, (u, v))) or int(n) != n: raise ValueError except ValueError: print('ValueError: All arguments should be integer, and the first two should be different and positive.') print((u, v, n), end = ' => ') n = int(abs(n)) if n > 0: u, v = map(lambda x:x if x > 0 else x + 1, map(lambda x:int(abs(x)), (u, v))) v = v if u != v else u + 1 finally: print((u, v, n), end = ': ') if n == 0: print([]) return [] seq = sorted([u, v]) seq2 = seq while len(seq) < n: # Test the first half of the sequence. To be proven? new = seq[-1] + 1 while len(list(filter(lambda i : new - seq[i] in seq and new != seq[i] + seq[i], range(len(seq) // 2 + (1 if len(seq) > 4 else 0))))) != 1: new += 1 seq.append(new) while len(seq2) < n: # Test the full sequence new = seq2[-1] + 1 while len(list(filter(lambda i : new - seq2[i] in seq2 and new != seq2[i] + seq2[i], range(len(seq2))))) != 2: new += 1 seq2.append(new) print(seq[:n] if seq == seq2 else (seq[:n], seq2[:n])) return seq[:n] if seq == seq2 else seq2[:n] ulam_sequence() ulam_sequence(4, 7, 0) ulam_sequence(-1.3, .5)
def ulam_sequence(u0, u1, n):u = [u0, u1]nn = u[-1] + 1while len(u) < n:count = 0for i in u:if nn - i in u and nn - i != i:count += 1if count >= 3:breaknn += 1if count == 2:u.append(nn)nn += 1return u- def ulam_sequence(u = 1, v = 2, n = 10):
- print()
- try:
- # Input manipulation just for fun :)
- if u == v or any(map(lambda x: int(x) != x or x < 1, (u, v))) or int(n) != n:
- raise ValueError
- except ValueError:
- print('ValueError: All arguments should be integer, and the first two should be different and positive.')
- print((u, v, n), end = ' => ')
- n = int(abs(n))
- if n > 0:
- u, v = map(lambda x:x if x > 0 else x + 1, map(lambda x:int(abs(x)), (u, v)))
- v = v if u != v else u + 1
- finally:
- print((u, v, n), end = ': ')
- if n == 0:
- print([])
- return []
- seq = sorted([u, v])
- seq2 = seq
- while len(seq) < n:
- # Test the first half of the sequence. To be proven?
- new = seq[-1] + 1
- while len(list(filter(lambda i : new - seq[i] in seq and new != seq[i]
- + seq[i], range(len(seq) // 2 + (1 if len(seq) > 4 else 0))))) != 1:
- new += 1
- seq.append(new)
- while len(seq2) < n:
- # Test the full sequence
- new = seq2[-1] + 1
- while len(list(filter(lambda i : new - seq2[i] in seq2 and new != seq2[i]
- + seq2[i], range(len(seq2))))) != 2:
- new += 1
- seq2.append(new)
- print(seq[:n] if seq == seq2 else (seq[:n], seq2[:n]))
- return seq[:n] if seq == seq2 else seq2[:n]
- ulam_sequence()
- ulam_sequence(4, 7, 0)
- ulam_sequence(-1.3, .5)