The smallest number a range of numbers can divide into – using Powershell

So there I was in work the other day talking maths and an interesting subject came up about calculating the lowest number that a range of numbers can divide into with no remainder. So I decided to try and solve the problem using powershell. There are a number of reasons why, but the main one was that I use it every day.

The problem states that 2520 is the smallest number that the range 1 to 10 can divide into without any remainders – what is the lowest number for the range 1 to 20.

So, at first I tried the brute force approach but found that this was too slow evening going from 1 to 10 – see below

Then I looked at the results and saw that prime numbers and prime factors were involved, so took another stab at it and cam up with the below, using 2 functions – isprimenumber and primefactors:


As you can see when you run this – its so much faster.


Leave a Reply

Your email address will not be published. Required fields are marked *