Skip to content

PrimeFactors

Anthony Lawrence edited this page Mar 11, 2018 · 2 revisions

Programming Concepts (Kata: Prime Factors)

Definition of Kata: An individual training exercise (in Karate or other martial arts).

This short teaching episode will teach the basic concepts of writing useful tests, developing using Test-Driven Development (TDD) and refactoring your code.

Pre-requisite knowledge

Let's go back to school for a moment and consider what a Prime Number is: any natural number greater than 1, that is divisble by itself and 1.

The first few prime numbers are therefore: 2,3,5,7,11,13,17,19,23,29,31,33,37.

If we consider what a Factor of a number that divdes exactly into another number. For example, the factors of 12 are: 1, 2, 3, 4, 6 and 12.

Combining these terms will result in Prime Factors being defined as:

All natural numbers, greater than 1, that are divisble by itself and one, which divide exactly into a given number.

Whilst that's a mouthful, let's review a few examples.

Example 1

The prime factors of 0 are nothing.

Example 2

The prime factors of 2 are: 2

Example 3

The prime factors of 3 are: 3

Example 4

The prime factors of 5 are: 5

Example 5

The prime factors of 6 are: 2, 3

Example 6

The prime factors of 10 are: 2, 5

Example 7

The prime factors of 12 are: 2, 2, 3

But why?

Well, we first check if 12 is divisible by 2 (which it is). As such, we know that 2 is a prime factor of 12.

Dividing 12 by 2, leaves us with 6. 6 is sadly not a prime number, so we break it down into its prime factors of 2 and 3.

Example 8

The prime factors of 50 are: 2, 5, 5