book cover
book cover

Programming Projects

Maths: Arithmetic

maintained by Francis Glassborow

  Home     

Treble and Add One if Odd [10B]

Contributed by Francis Glassborow

Start with any number. If the number is odd, multiply by three and add one. If it is even divide by two. Repeat the process until you repeat an earlier number. E.g. if you started witt 1 you would get the sequence 1, 4, 2, 1. If you started with 3 you would generate 3, 10, 5, 16, 8, 4, 2, 1, 4.

Write a program that will count the number of elements in the sequence generated by each number less than some maximum determined by the user and output the longest sequence. How many sequences are required to cover all the numbers less than a given value? For example only one sequence is necessary to cover the numbers up to 2 (1, 4, 2, 1 does that). Note that the second sequence above covers all numbers up to 5. However by starting with 6 we get all numbers up to 6 in a single sequence.

Program elements

This one requires only simple arithmetic but you will need to think about how to manage the computed data.


  Home      Arithmetic      Maths      You Can Do It!