1. Fraction sequence

VC2M6A03: level 6: Design and use algorithms involving a sequence of steps and decisions that use rules to generate sets of numbers; identify, interpret and explain emerging patterns
  • using an algorithm to create extended number sequences involving rational numbers, using a rule and digital tools, and explaining any emerging patterns


1.1. Fraction sequence

Fraction sequence returns the fractions as strings.
 1from fractions import Fraction
 2
 3def generate_rational_sequence(denom, start, end, step):
 4    sequence = []
 5    for i in range(start, end, step):
 6        # Create a Fraction object for each number in the sequence
 7        rational_number = Fraction(i, denom)
 8        mixed_fraction = rational_number.limit_denominator(1)
 9        sequence.append(rational_number)
10        sequence.append(mixed_fraction)
11    return sequence
12
13# Generate a sequence of rational numbers from 1 to 10 with a step of 1
14sequence = generate_rational_sequence(3, 1, 11, 1)
15print("Generated sequence:", [str(num) for num in sequence])
16
17
Sample output is:
Generated sequence: ['1/3', '2/3', '1', '4/3', '5/3', '2', '7/3', '8/3', '3', '10/3']
The pattern is: Every third number is an integer.