comp.lang.ada
 help / color / mirror / Atom feed
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Subject: Re: project euler 26
Date: Tue, 05 Sep 2023 00:16:47 +0100	[thread overview]
Message-ID: <8734ztttpc.fsf@bsb.me.uk> (raw)
In-Reply-To: ud5gh7$1kigc$1@dont-email.me

"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> On 2023-09-04 22:18, Ben Bacarisse wrote:
>> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
>> 
>>> On 2023-09-04 18:01, Ben Bacarisse wrote:
>>>> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> 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.

  reply	other threads:[~2023-09-04 23:16 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 [this message]
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
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