18 – Classes, Objects, & Inheritance

18 – Classes, Objects, & Inheritance

Welcome to the masterclass on Object Oriented Programming (Classes, Objects, & Inheritance). As this topic requires some practice, please use the video above as an introduction and make use of the Easter break to try out the programming challenges below.

Challenge 1

For every positive number n we define the function streak(n)=k as the smallest positive integer k such that n+k is not divisible by k+1.
13 is divisible by 1
14 is divisible by 2
15 is divisible by 3
16 is divisible by 4
17 is NOT divisible by 5
So streak(13) = 4.
120 is divisible by 1
121 is NOT divisible by 2
So streak(120) = 1.

Define P(s, N) to be the number of integers n, 1 < n < N, for which streak(n) = s.
So P(3, 14) = 1 and P(6, 10^6) = 14286.

Find the sum, as i ranges from 1 to 31, of P(i, 4^i).

Challenge 2

A non-decreasing sequence of integers a_n can be generated from any positive real value theta by the following procedure: begin{align} begin{split} b_1 &= theta b_n &= leftlfloor b_{n-1} rightrfloor left(b_{n-1} – leftlfloor b_{n-1} rightrfloor + 1right)~~~forall ~ n geq 2 a_n &= leftlfloor b_{n} rightrfloor end{split} end{align} Where leftlfloor . rightrfloor is the floor function.

For example, theta=2.956938891377988... generates the Fibonacci sequence: 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

The concatenation of a sequence of positive integers a_n is a real value denoted tau constructed by concatenating the elements of the sequence after the decimal point, starting at a_1: a_1.a_2a_3a_4...

For example, the Fibonacci sequence constructed from theta=2.956938891377988... yields the concatenation tau=2.3581321345589... Clearly, tau neq theta for this value of theta.

Find the only value of theta for which the generated sequence starts at a_1=2 and the concatenation of the generated sequence equals the original value: tau = theta. Give your answer rounded to 24 places after the decimal point.

Extension Challenges

HackerRank holds a number of online programming challenges across a number of languages. Use the links below to try out the Object Oriented Python challenges (or use the site to find your chosen programming language)

Challenge 1 – Find the Torsional Angle

Challenge 2 – Dealing With Complex Numbers

Challenges reproduced with thanks to https://projecteuler.net