Task: Abundant odd numbers








VB: Module AbundantOddNumbers
    
    
    

    
    Private Function divisorSum(n As Integer) As Integer
        Dim sum As Integer = 1
        For d As Integer = 2 To Math.Round(Math.Sqrt(n))
            If n Mod d = 0 Then
                sum += d
                Dim otherD As Integer = n \ d
                IF otherD <> d Then
                    sum += otherD
                End If
            End If
        Next d
        Return sum
    End Function

    
    Public Sub Main(args() As String)
        
        Dim oddNumber As Integer = 1
        Dim aCount As Integer = 0
        Dim dSum As Integer = 0
        Console.Out.WriteLine("The first 25 abundant odd numbers:")
        Do While aCount < 25
            dSum = divisorSum(oddNumber)
            If dSum > oddNumber Then
                aCount += 1
                Console.Out.WriteLine(oddNumber.ToString.PadLeft(6) & " proper divisor sum: " & dSum)
            End If
            oddNumber += 2
        Loop
        
        Do While aCount < 1000
            dSum = divisorSum(oddNumber)
            If dSum > oddNumber Then
                aCount += 1
            End If
            oddNumber += 2
        Loop
        Console.Out.WriteLine("1000th abundant odd number:")
        Console.Out.WriteLine("    " & (oddNumber - 2) & " proper divisor sum: " & dSum)
        
        oddNumber = 1000000001
        Dim found As Boolean = False
        Do While Not found
            dSum = divisorSum(oddNumber)
            If dSum > oddNumber Then
                found = True
                Console.Out.WriteLine("First abundant odd number > 1 000 000 000:")
                Console.Out.WriteLine("    " & oddNumber & " proper divisor sum: " & dSum)
            End If
            oddNumber += 2
        Loop
    End Sub
End Module




Ada: 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
   
   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;
   
   
   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);
   
   
   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;