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=-1.9 required=3.0 tests=BAYES_00,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: Ben Bacarisse Newsgroups: comp.lang.ada Subject: Re: project euler 26 Date: Tue, 05 Sep 2023 00:16:47 +0100 Organization: A noiseless patient Spider Message-ID: <8734ztttpc.fsf@bsb.me.uk> References: <878r9mudvj.fsf@bsb.me.uk> <87a5u1u1yv.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: dont-email.me; posting-host="2907f66c348ba0da34ad87ef57ce8447"; logging-data="1759040"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+RQDBfB92OUYAWvCH9APAt4VTlzdNUmzg=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) Cancel-Lock: sha1:4ESW1qH9MX/UZuzufgw9V/N6uUU= sha1:3DgbzGENaZ4xRVS6xJ33vwO9GH0= X-BSB-Auth: 1.767e128e93d1655732ff.20230905001647BST.8734ztttpc.fsf@bsb.me.uk Xref: news.eternal-september.org comp.lang.ada:65595 List-Id: "Dmitry A. Kazakov" writes: > On 2023-09-04 22:18, Ben Bacarisse wrote: >> "Dmitry A. Kazakov" writes: >> >>> On 2023-09-04 18:01, Ben Bacarisse wrote: >>>> "Dmitry A. Kazakov" writes: >>>> >>>>> BTW, Ada is perfect for numeric algorithms no need to resort to functional >>>>> mess... (:-)) >>>> Perfect? That's a bold claim! >>> >>> Ada is a very improved descendant of Algol 60, which was designed to codify >>> algorithms. >> Yes, though I was respond to you narrower remark about being perfect for >> numeric algorithms. > > Yes, Ada is. :-) >>> (rather than about for example building >>> abstractions as in the case of OOP) >> >> That's interesting. You don't consider using functions and procedures >> (possibly higher-order ones) to be a way to build abstractions? > > No, they do not introduce new types and do not form some structure of their > values. And "using" is not an abstraction anyway. The term "abstraction" is usually taken to be more general than that so as to include function (or procedural) abstraction. Ada is good at that, but the syntax is sufficiently cumbersome that I think it discourages people from exploiting that part of the language. Mind you, I am no Ada expert so maybe it's simpler to do than I think. Here's my Ada solution: with Ada.Text_IO; use Ada.Text_IO; with Ada.Containers.Ordered_Maps; use Ada.Containers; procedure Euler_26 is function Period(Divisor: Positive) return Positive is Index: Natural := 0; Carry: Natural := 1; package Carry_Maps is new Ordered_Maps(Natural, Natural); use Carry_Maps; Carries: Map; Loc: Cursor; begin loop Loc := Carries.Find(Carry); exit when Loc /= No_Element; Carries.Insert(Carry, Index); Index := Index + 1; Carry := Carry mod Divisor * 10; end loop; return Index - Element(Loc); end Period; Max_Period: Natural := 1; Divisor_With_Max_Period: Natural := 1; begin for D in 2..999 loop declare Ds_Period: constant Positive := Period(D); begin if Ds_Period > Max_Period then Divisor_With_Max_Period := D; Max_Period := Ds_Period; end if; end; end loop; Put_Line(Integer'Image(Divisor_With_Max_Period)); end Euler_26; The part that finds the D that maximises Period(D) is just boilerplate code. I know this can be abstracted out in Ada, but I think the syntax is messy. I was hoping to find (or be able to write) a generic function that takes an 'iterable' (if that's the right word) and a function, and which returns the element that maximises the function. I got stuck trying. Maybe someone can help? I know it won't make this program shorter, but it would be interesting to know how it might be done. -- Ben.