From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-3.2 required=3.0 tests=BAYES_00,NICE_REPLY_A, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: project euler 26 Date: Mon, 4 Sep 2023 16:23:54 +0200 Organization: A noiseless patient Spider Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 4 Sep 2023 14:23:54 -0000 (UTC) Injection-Info: dont-email.me; posting-host="84abbd2dc2e4fea6082302e85e06124d"; logging-data="1597027"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SyZPw/eTT+M4wWtadMRTu5rPMnBB11Do=" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Cancel-Lock: sha1:UeQia4wcDxvTGSHH8VNeLkDFUvo= In-Reply-To: Content-Language: en-US Xref: news.eternal-september.org comp.lang.ada:65590 List-Id: 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