PageRenderTime 24ms CodeModel.GetById 16ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/five.adb

http://github.com/rtyler/euler-ada
Ada | 42 lines | 25 code | 6 blank | 11 comment | 4 complexity | 17002b9431f1578c43b234d730e5ce2a MD5 | raw file
 1--
 2--  Project Euler problem #5
 3--
 4--  2520 is the smallest number that can be divided by each of the numbers from
 5--  1 to 10 without any remainder.
 6--
 7--  What is the smallest positive number that is evenly divisible by all of the
 8--  numbers from 1 to 20?
 9
10with Ada.Text_IO;
11
12use Ada.Text_IO;
13
14procedure Five is
15    Valid_Numbers : array (1 .. 20) of Natural;
16    --  Stepping up with the highest number we need to be a multiple of
17    Step : constant Natural := Valid_Numbers'Last;
18    Should_Continue : Boolean := true;
19    Value : Natural := 0;
20
21    --  Divisible_By_Set will return early with false as soon as it hits on a
22    --  number that is not cleanly divisible (i.e. no remainer)
23    function Divisible_By_Set (Number : in Natural) return Boolean is
24    begin
25        for Item in Valid_Numbers'Range loop
26            if (Number mod Item) /= 0 then
27                return false;
28            end if;
29        end loop;
30        return true;
31    end Divisible_By_Set;
32begin
33    while Should_Continue loop
34        Value := (Value + Step);
35
36        if Divisible_By_Set (Value) then
37            Should_Continue := false;
38        end if;
39    end loop;
40
41    Put_Line (Natural'Image (Value));
42end Five;