comp.lang.ada
 help / color / mirror / Atom feed
From: Wes Groleau <wwgrol@ftw.rsc.raytheon.com>
Subject: Re: Sums of cubes (was Re: histrionics)
Date: 1999/09/24
Date: 1999-09-24T00:00:00+00:00	[thread overview]
Message-ID: <37EB958C.26DB4C97@ftw.rsc.raytheon.com> (raw)
In-Reply-To: 37EA8DEA.61D49E6A@mitre.org

> > How would you find all numbers like 407=4^3+0^3+7^3 that are equal to the
> > sum of their digits' cubes?

Not as efficient as Robert Eachus's example, but IMHO, clearer and more
general:

    generic
      type Nat is range <>;
    procedure Sum_of_Cubes;

    with Ada.Text_IO;
    procedure Sum_of_Cubes is

      Cube : array (Character range '0'..'9') of Nat :=
                      (0, 1, 8, 27, 64, 125, 216, 343, 512, 729);

      Counter : Nat := 0;


      function Sum_Of_Cubes_Matches (N : Nat) return Boolean is

         N_Str : constant String := Nat'Image (N);
         Sum   :          Nat    := Nat'First;
    
      begin

         -- skip the non-digit first character of 'Image

         for I in N_Str'First + 1 .. N_Str'Last loop

            Sum := Sum + Cube (N_Str(I));

        end loop;

        return (abs N) = Sum;  -- ignore sign

      end Sum_Of_Cubes_Matches;


    begin  -- Sum_Of_Cubes

      Find_Numbers:
        loop  

          Check_One:
            begin

            if Sum_Of_Cubes_Matches (Counter) then
            -- no semicolon after conditional expression :-)
               Ada.Text_IO.Put_Line (Nat'Image (Counter));
            end if;

            exception
              when Constraint_Error =>
                null;  -- If the sum gets a constraint error
                       -- and the number didn't, it's not a match.
                       -- In a block because we don't want an
                       -- error on 9,999 to prevent checking 10,000
                       -- (I think Robert missed this detail.)
            end Check_One;

            exit Find_Numbers when Counter := Nat'Last;

            Counter := Counter + 1;

        end loop Find_Numbers;

    end Sum_Of_Cubes;

Sacrificing clarity for efficiency is not usually good, though it might
be worthwhile in this case.  (Takes ten seconds to do 0..999_999 on a
366 MHz
SPARC--takes longer than one can endure to do Natural'Range in GNAT).
I'm sure 'Image on every number has a big impact.




  reply	other threads:[~1999-09-24  0:00 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-09  0:00 histrionics G
1999-09-08  0:00 ` histrionics Preben Randhol
1999-09-09  0:00   ` histrionics G
1999-09-09  0:00     ` histrionics Nick Roberts
1999-09-09  0:00       ` histrionics Robert Dewar
1999-09-10  0:00         ` histrionics Vladimir Olensky
1999-09-10  0:00           ` histrionics Robert Dewar
1999-09-10  0:00             ` histrionics Ted Dennison
1999-09-11  0:00               ` histrionics Bob Collins
1999-09-12  0:00                 ` histrionics Vladimir Olensky
1999-09-13  0:00                 ` histrionics Ted Dennison
1999-09-11  0:00             ` histrionics Vladimir Olensky
1999-09-11  0:00               ` histrionics Robert Dewar
1999-09-11  0:00                 ` histrionics Vladimir Olensky
1999-09-14  0:00                 ` histrionics Robert I. Eachus
     [not found]                   ` <7s2l7b$kmr$1@nnrp1.deja.com>
     [not found]                     ` <37E81661.6DCA23E4@mitre.org>
1999-09-22  0:00                       ` histrionics p.obry
1999-09-22  0:00                       ` histrionics Ehud Lamm
     [not found]                       ` <7saju5$6h6$1@nnrp1.deja.com>
1999-09-22  0:00                         ` histrionics Robert I. Eachus
1999-09-23  0:00                   ` histrionics Ehud Lamm
1999-09-23  0:00                     ` histrionics Ehud Lamm
1999-09-23  0:00                     ` Sums of cubes (was Re: histrionics) Robert I. Eachus
1999-09-24  0:00                       ` Wes Groleau [this message]
1999-09-24  0:00                         ` Robert I. Eachus
1999-09-27  0:00                           ` Wes Groleau
1999-09-24  0:00                       ` Robert Dewar
1999-09-24  0:00                         ` Robert I. Eachus
1999-09-24  0:00                       ` Wes Groleau
1999-09-25  0:00                         ` Robert Dewar
1999-09-11  0:00               ` histrionics Robert Dewar
1999-09-11  0:00                 ` histrionics Vladimir Olensky
1999-09-13  0:00                   ` histrionics Robert Dewar
replies disabled

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