comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: project euler 26
Date: Mon, 4 Sep 2023 16:23:54 +0200	[thread overview]
Message-ID: <ud4p9q$1gnj3$1@dont-email.me> (raw)
In-Reply-To: <c343abf6-3d20-4493-90e5-a317c6829cb2n@googlegroups.com>

On 2023-09-04 11:19, CSYH (QAQ) wrote:
> I am new to Ada, I know is there a good way to start this program?
> thanks
> https://projecteuler.net/problem=26

Ok, I leave it to you checking if my implementation is correct.
-------------------test.adb----------
with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
with Ada.Text_IO;            use Ada.Text_IO;

procedure Test is

    N : constant := 1000;

    function Period (Divisor : Positive) return String is
       type Remainder is record
          Index : Positive;
          Value : Positive;
       end record;
       Result     : String (1..N);
       Value      : Integer := 1;
       Remainders : array (1..N) of Remainder;
    begin
       for Index in Result'Range loop
          Value  := Value * 10;
          Result (Index) :=
             Character'Val (Character'Pos ('0') + Value / Divisor);
          Value := Value mod Divisor;
          if Value = 0 then
             return ""; -- Non-periodic
          end if;
          if Index > 1 then
             for Item in 1..Index - 1 loop
                declare
                   This : Remainder renames Remainders (Item);
                begin
                   if Value = This.Value then
                      return Result (This.Index + 1..Index);
                   end if;
                end;
             end loop;
          end if;
          Remainders (Index) := (Index, Value);
       end loop;
       raise Constraint_Error with "Period calculation error";
    end Period;

    Max_Period  : Unbounded_String;
    Max_Divisor : Positive;
begin
    for Divisor in 2..999 loop
       declare
          This : constant String := Period (Divisor);
       begin
          if This /= "" then
             Put_Line
             (  "1 /"
             &  Integer'Image (Divisor)
             &  " has "
             &  This
             &  " in period"
             );
          end if;
          if Length (Max_Period) < This'Length then
             Max_Period  := To_Unbounded_String (This);
             Max_Divisor := Divisor;
          end if;
       end;
    end loop;
    Put_Line
    (  "The first longest period is "
    &  To_String (Max_Period)
    &  " in 1 /"
    &  Integer'Image (Max_Divisor)
    );
end Test;
-------------------test.adb----------
It gives the longest period for 1/983.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  parent reply	other threads:[~2023-09-04 14:23 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-04  9:19 project euler 26 CSYH (QAQ)
2023-09-04 11:06 ` Niklas Holsti
2023-09-04 12:39   ` Dmitry A. Kazakov
2023-09-04 16:01     ` Ben Bacarisse
2023-09-04 19:20       ` Dmitry A. Kazakov
2023-09-04 20:18         ` Ben Bacarisse
2023-09-04 21:00           ` Dmitry A. Kazakov
2023-09-04 23:16             ` Ben Bacarisse
2023-09-05  7:23               ` Dmitry A. Kazakov
2023-09-05 15:18                 ` Ben Bacarisse
2023-09-05 17:08                   ` Dmitry A. Kazakov
2023-09-06  1:10                     ` Ben Bacarisse
2023-09-06  7:06                       ` Dmitry A. Kazakov
2023-09-06 15:16                         ` Ben Bacarisse
2023-09-06 15:54                           ` Dmitry A. Kazakov
2023-09-06 23:32                             ` Ben Bacarisse
2023-09-07  9:02                               ` Dmitry A. Kazakov
2023-09-08  1:32                                 ` Ben Bacarisse
2023-09-08  7:23                                   ` Dmitry A. Kazakov
2023-09-09  0:25                                     ` Ben Bacarisse
2023-09-09  9:32                                       ` Dmitry A. Kazakov
2023-09-10  1:20                                         ` Ben Bacarisse
2023-09-10  8:46                                           ` Dmitry A. Kazakov
2023-09-10 19:22                                             ` Ben Bacarisse
2023-09-11  6:53                                               ` Dmitry A. Kazakov
2023-09-11 16:13                                                 ` Ben Bacarisse
2023-09-12  7:17                                                   ` Dmitry A. Kazakov
2023-09-13 12:24                                                     ` Ben Bacarisse
2023-09-14  6:33                                                       ` Dmitry A. Kazakov
2023-09-14 14:30                                                         ` Ben Bacarisse
2023-09-08  6:09                               ` G.B.
2023-09-08 21:02                                 ` Ben Bacarisse
2023-09-09  8:13                                   ` G.B.
2023-09-09 21:04                                     ` Ben Bacarisse
2023-09-10  9:11                                     ` Dmitry A. Kazakov
2023-09-05 17:35                 ` moi
2023-09-04 14:23 ` Dmitry A. Kazakov [this message]
2023-09-07  7:31 ` Francesc Rocher
2023-09-15  9:07   ` CSYH (QAQ)
2023-09-19  7:59     ` comp.lang.ada
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox