How to resolve the algorithm Abundant odd numbers step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abundant odd numbers step by step in the Ada programming language

Table of Contents

Problem Statement

An Abundant number is a number n for which the   sum of divisors   σ(n) > 2n, or,   equivalently,   the   sum of proper divisors   (or aliquot sum)       s(n) > n.

12   is abundant, it has the proper divisors     1,2,3,4 & 6     which sum to   16   ( > 12 or n);        or alternately,   has the sigma sum of   1,2,3,4,6 & 12   which sum to   28   ( > 24 or 2n).

Abundant numbers are common, though even abundant numbers seem to be much more common than odd abundant numbers. To make things more interesting, this task is specifically about finding   odd abundant numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abundant odd numbers step by step in the Ada programming language

Source code in the ada programming language

with Ada.Text_IO, Generic_Divisors;

procedure Odd_Abundant is
   function Same(P: Positive) return Positive is (P);
   
   package Divisor_Sum is new Generic_Divisors
     (Result_Type => Natural, None => 0, One => Same, Add =>  "+");
   
   function Abundant(N: Positive) return Boolean is
      (Divisor_Sum.Process(N) > N);
      
   package NIO is new Ada.Text_IO.Integer_IO(Natural);
   
   Current: Positive := 1;
   
   procedure Print_Abundant_Line
     (Idx: Positive; N: Positive; With_Idx: Boolean:= True) is
   begin
      if With_Idx then 
	 NIO.Put(Idx, 6);  Ada.Text_IO.Put(" |");
      else
	 Ada.Text_IO.Put("   *** |");
      end if;
      NIO.Put(N, 12); Ada.Text_IO.Put(" | "); 
      NIO.Put(Divisor_Sum.Process(N), 12); Ada.Text_IO.New_Line;
   end Print_Abundant_Line;      
   
begin
   -- the first 25 abundant odd numbers
   Ada.Text_IO.Put_Line(" index |      number | proper divisor sum ");
   Ada.Text_IO.Put_Line("-------+-------------+--------------------");
   for I in 1 .. 25 loop
      while not Abundant(Current) loop
	 Current := Current + 2;
      end loop;
      Print_Abundant_Line(I, Current);
      Current := Current + 2;
   end loop;
   
   -- the one thousandth abundant odd number
   Ada.Text_IO.Put_Line("-------+-------------+--------------------");
   for I in 26 .. 1_000 loop
      Current := Current + 2;
      while not Abundant(Current) loop
	 Current := Current + 2;
      end loop;
   end loop;
   Print_Abundant_Line(1000, Current);
   
   -- the first abundant odd number greater than 10**9
   Ada.Text_IO.Put_Line("-------+-------------+--------------------");
   Current := 10**9+1;
   while not Abundant(Current) loop
      Current := Current + 2;
   end loop;
   Print_Abundant_Line(1, Current, False);
end Odd_Abundant;


  

You may also check:How to resolve the algorithm Pangram checker step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Draco programming language
You may also check:How to resolve the algorithm Power set step by step in the Tcl programming language
You may also check:How to resolve the algorithm ISBN13 check digit step by step in the Delphi programming language
You may also check:How to resolve the algorithm Minimum multiple of m where digital sum equals m step by step in the Pascal programming language