How to resolve the algorithm Proper divisors step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Proper divisors step by step in the Ada programming language

Table of Contents

Problem Statement

The   proper divisors   of a positive integer N are those numbers, other than N itself, that divide N without remainder. For N > 1 they will always include 1,   but for N == 1 there are no proper divisors.

The proper divisors of     6     are   1, 2, and 3. The proper divisors of   100   are   1, 2, 4, 5, 10, 20, 25, and 50.

Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Proper divisors step by step in the Ada programming language

Source code in the ada programming language

generic
   type Result_Type (<>) is limited private;
   None: Result_Type;
   with function One(X: Positive) return Result_Type;
   with function Add(X, Y: Result_Type) return Result_Type 
      is <>;
package Generic_Divisors is
  
  function Process
    (N: Positive; First: Positive := 1) return Result_Type is      
      (if First**2 > N or First = N then None 
      elsif (N mod First)=0 then 
	(if First = 1 or First*First = N 
	   then Add(One(First), Process(N, First+1))
	   else Add(One(First), 
		    Add(One((N/First)), Process(N, First+1))))
      else Process(N, First+1));
      
end Generic_Divisors;


with Ada.Text_IO, Ada.Containers.Generic_Array_Sort, Generic_Divisors;

procedure Proper_Divisors is
   
begin
   -- show the proper divisors of the numbers 1 to 10 inclusive.
   declare
      type Pos_Arr is array(Positive range <>) of Positive;
      subtype Single_Pos_Arr is Pos_Arr(1 .. 1);
      Empty: Pos_Arr(1 .. 0); 
      
      function Arr(P: Positive) return Single_Pos_Arr is ((others => P));
      
      package Divisor_List is new Generic_Divisors
	(Result_Type => Pos_Arr, None => Empty, One => Arr, Add =>  "&");
      
      procedure Sort is new Ada.Containers.Generic_Array_Sort
	(Positive, Positive, Pos_Arr);
   begin
      for I in 1 .. 10 loop
	 declare
	    List: Pos_Arr := Divisor_List.Process(I);
	 begin
	    Ada.Text_IO.Put
	      (Positive'Image(I) & " has" & 
		 Natural'Image(List'Length) & " proper divisors:");
	    Sort(List);
	    for Item of List loop
	       Ada.Text_IO.Put(Positive'Image(Item));
	    end loop;
	    Ada.Text_IO.New_Line;
	 end;
      end loop;
   end;
   
   -- find a number 1 .. 20,000 with the most proper divisors
   declare
      Number: Positive := 1;
      Number_Count: Natural := 0;
      Current_Count: Natural;
      
      function Cnt(P: Positive) return Positive is (1);
   
      package Divisor_Count is new Generic_Divisors
	(Result_Type => Natural, None => 0, One => Cnt, Add =>  "+");
      
   begin
      for Current in 1 .. 20_000 loop
	 Current_Count := Divisor_Count.Process(Current);
	 if Current_Count > Number_Count then
	    Number := Current;
	    Number_Count := Current_Count;
	 end if;
      end loop;
      Ada.Text_IO.Put_Line
	(Positive'Image(Number) & " has the maximum number of" & 
	   Natural'Image(Number_Count) & " proper divisors.");
   end;
end Proper_Divisors;


  

You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the D programming language
You may also check:How to resolve the algorithm Factorial step by step in the Oberon programming language
You may also check:How to resolve the algorithm Send email step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Subleq step by step in the Arturo programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Lasso programming language