comp.lang.ada
 help / color / mirror / Atom feed
* help with starting a program (newbie)
@ 2006-02-16 21:47 isaac2004
  2006-02-17  9:28 ` Stephen Leake
  0 siblings, 1 reply; 4+ messages in thread
From: isaac2004 @ 2006-02-16 21:47 UTC (permalink / raw)


hello i am in a basic ada class and i have an assignment that asks to
find all sociable chains in a range defined by the user. A sociable
chain is a generalization of the notion of amicable pairs. More
precisely, a sociable chain is sequence of positive integers,  like
I(1), I(2), .. I(N)
in which (1) each element I (i) is equal to the sum of the proper
divisors of I(i-1) , (2)
I (N) = I (1) and (3) none of the integers between the I (1) and I (N)
are equal (if there are any
integers between the first and the last; for chains of length two,
i.e., perfect numbers, of
course there won't be). this is the thoery behind sociable chains.

the program will ask for 3 inputs from a user, them being a start of
the number range, an end of the numebr range, and finally a maximum
chain length.

Sample Run of Program
-----------------------------------
Start = 12000
Stop = 15000
Length = 30

after input program computes all possible chains in the range of
numbers with a maximum chain length of 30 numbers
example
This chain is 3 in
length.
12285
14595
12285
This chain is 6 in
length.
12496
14288
15472
14536
14264
12496
This chain is 6 in
length.
14264
12496
14288
15472
14536
14264
This chain is 6 in
length.
14288
15472
14536
14264
12496
14288
This chain is 29 in
length.
14316
19116
31704
47616
83328
177792
295488
629072
589786
294896
358336
418904
366556
274924
275444
243760
376736
381028
285778
152990
122410
97946
48976
45946
22976
22744
19916
17716
14316
This chain is 6 in
length.
14536
14264
12496
14288
15472
14536
This chain is 3 in
length.
14595
12285
14595

------------------------------------------------\
the program uses the idea from this function
function Sum_Of_Divisors (
         N : Natural )
     return Natural is

         Sum : Natural;

   begin
Sum := 0;
      for I in 1 .. N - 1 loop
           if  N rem I = 0 then
            Sum := Sum + I;
                 end if;
      end loop;
      return Sum;
   end Sum_Of_Divisors;

this program finds the sum of the divisors of any number entered. any
help with this program would be greatly appreciated. thank you

isaac




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: help with starting a program (newbie)
  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>
  0 siblings, 1 reply; 4+ messages in thread
From: Stephen Leake @ 2006-02-17  9:28 UTC (permalink / raw)


"isaac2004" <isaac_2004@yahoo.com> writes:

> hello i am in a basic ada class and i have an assignment that asks to
> find all sociable chains in a range defined by the user. 

That's good. Welcome to the wonderful world of Ada.

<snip>

> this program finds the sum of the divisors of any number entered. any
> help with this program would be greatly appreciated. 

We won't do your homework for you; that's not a good way for you to
learn Ada.

If you make an attempt, and get stuck, and post a small piece of code
here, we will help you understand why it doesn't work.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: help with starting a program (newbie)
       [not found]   ` <1140193368.725447.184020@g44g2000cwa.googlegroups.com>
@ 2006-02-17 18:05     ` isaac2004
  2006-02-17 23:59       ` Björn Persson
  0 siblings, 1 reply; 4+ messages in thread
From: isaac2004 @ 2006-02-17 18:05 UTC (permalink / raw)


> If you make an attempt, and get stuck, and post a small piece of code
here, we will help you understand why it doesn't work.


well i have a little code here that defines the Sum_Of_Devisors and
Sociable Chain procedures

here is the code i have so far

with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Integer_Text_Io;
use Ada.Integer_Text_Io;
procedure Sociablechain is

---------------------------------------------------------------------
   --| program that takes a range in the form of a minimum and maximum
number,
   --| and prompts for the length, to find all sociable chains inside
of the range
   --| to that desired chain
   --| Isaac Levin, Western Washington
   --| February 2006

---------------------------------------------------------------------------

   Minnumber : Natural range 0 .. Integer'Last;
   Maxnumber : Natural range Minnumber .. Integer'Last;
    Length : Natural range 1 .. 30;




   -- start of function that takes the inputted number and finds the
sum of the divisors
   -- function description
   function Sum_Of_Divisors (
         N : Natural )
     return Natural;

   -- function body

   function Sum_Of_Divisors (
         N : Natural )
     return Natural is

      Sum : Natural;

   begin
      Sum := 0;
      for I in 1 .. N - 1 loop
         if  N rem I = 0 then
            Sum := Sum + I;
         end if;
      end loop;
      return Sum;
   end Sum_Of_Divisors;




   procedure Sociable_Chain (
         Minnumber,
         Maxnumber  : Natural ) is
      A : array (1 .. 30) of Positive;
      I : Positive;

   begin
      A(1) :=Minnumber;
      A(2) := Sum_Of_Divisors (A(1));
      while I < Length and A (1) /= A(I) loop
         I:= I + 1;
         A(I) := Sum_Of_Divisors (A(I - 1));


      end loop;

end Sociable_Chain;

      begin

         for N in Minnumber..Maxnumber loop
            Sociable_Chain(Minnumber, Length);











         end loop;



           Put(Item => " Please enter your starting integer. ");
          Get(Item => Minnumber);
          New_Line;
          Put(Item => " Please enter your ending integer. ");
          Get(Item => Maxnumber);
          New_Line;
          Put(Item => " Please choose the length of your chain. ");
          Get(Item => Length);






      end; 
   
its sloppy but thats how i am thinking right now




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: help with starting a program (newbie)
  2006-02-17 18:05     ` isaac2004
@ 2006-02-17 23:59       ` Björn Persson
  0 siblings, 0 replies; 4+ messages in thread
From: Björn Persson @ 2006-02-17 23:59 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-02-17 23:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox