From: "Björn Persson" <spam-away@nowhere.nil>
Subject: Re: help with starting a program (newbie)
Date: Fri, 17 Feb 2006 23:59:01 GMT
Date: 2006-02-17T23:59:01+00:00 [thread overview]
Message-ID: <9rtJf.45284$d5.201678@newsb.telia.net> (raw)
In-Reply-To: <1140199513.110445.267520@g44g2000cwa.googlegroups.com>
You seem to be on the right track. What's missing is mostly the code for
printing the chains when they're found. Your approach seems to be that
for each number in the range you calculate a sequence of sums of
divisors and see whether it wraps around before it exceeds the maximum
length. This might not be the fastest method and it will find the same
chain multiple times, but I see that the sample run you posted does list
the same chains multiple times, so apparenty this is acceptable.
> Minnumber : Natural range 0 .. Integer'Last;
This is exactly the same as "Minnumber : Natural;".
> Maxnumber : Natural range Minnumber .. Integer'Last;
You're trying to codify that Maxnumber must not be less than Minnumber.
This is not a bad idea, but then you have to know the value of Minnumber
before you can declare Maxnumber. You have to do it like this:
procedure Sociablechain is
-- other code here
Minnumber : Natural;
-- We can't reference Minnumber yet.
begin
Put(Item => " Please enter your starting integer. ");
Get(Item => Minnumber);
New_Line;
declare
-- Now Minnumber has a value.
Maxnumber : Natural range Minnumber .. Integer'Last;
begin
Put(Item => " Please enter your ending integer. ");
Get(Item => Maxnumber);
New_Line;
-- other code here
end;
end Sociablechain;
> Length : Natural range 1 .. 30;
Is it specified that the program shouldn't accept lengths greater than
30? If not, you should probably allow much greater lengths.
> function Sum_Of_Divisors (
[...]
> for I in 1 .. N - 1 loop
Numbers greater than half of N can't very well be divisors of N. You
could cut the execution time in half by looping over 1 .. N / 2 instead.
> procedure Sociable_Chain (
> Minnumber,
> Maxnumber : Natural ) is
You need to think more about the parameters to this procedure. What do
you need to know to start calculating a chain?
> A : array (1 .. 30) of Positive;
> I : Positive;
You could let the upper bound of A be the maximum length that the user
entered instead of a fixed number. Then only the computer's memory would
limit how long chains you could handle.
A and I should have more descriptive names.
>
> begin
> A(1) :=Minnumber;
Here Minnumber seems to be a starting point rather than a minimum.
> A(2) := Sum_Of_Divisors (A(1));
> while I < Length and A (1) /= A(I) loop
What value will I have the first time you reach this point?
> I:= I + 1;
> A(I) := Sum_Of_Divisors (A(I - 1));
>
>
> end loop;
Now you need to find out whether the loop ended because a sociable chain
was found or because the maximum length was exceeded � and if a chain
was found you know what to do with it, don't you?
>
> end Sociable_Chain;
--
Bj�rn Persson PGP key A88682FD
omb jor ers @sv ge.
r o.b n.p son eri nu
prev parent reply other threads:[~2006-02-17 23:59 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-02-16 21:47 help with starting a program (newbie) isaac2004
2006-02-17 9:28 ` Stephen Leake
[not found] ` <1140193368.725447.184020@g44g2000cwa.googlegroups.com>
2006-02-17 18:05 ` isaac2004
2006-02-17 23:59 ` Björn Persson [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox