Task: Attractive numbers




Go: package main

import "fmt"

func isPrime(n int) bool {
    switch {
    case n < 2:
        return false
    case n%2 == 0:
        return n == 2
    case n%3 == 0:
        return n == 3
    default:
        d := 5
        for d*d <= n {
            if n%d == 0 {
                return false
            }
            d += 2
            if n%d == 0 {
                return false
            }
            d += 4
        }
        return true
    }
}

func countPrimeFactors(n int) int {
    switch {
    case n == 1:
        return 0
    case isPrime(n):
        return 1
    default:
        count, f := 0, 2
        for {
            if n%f == 0 {
                count++
                n /= f
                if n == 1 {
                    return count
                }
                if isPrime(n) {
                    f = n
                }
            } else if f >= 3 {
                f += 2
            } else {
                f = 3
            }
        }
        return count
    }
}

func main() {
    const max = 120
    fmt.Println("The attractive numbers up to and including", max, "are:")
    count := 0
    for i := 1; i <= max; i++ {
        n := countPrimeFactors(i)
        if isPrime(n) {
            fmt.Printf("%4d", i)
            count++
            if count % 20 == 0 {
                fmt.Println()
            }
        }       
    }
    fmt.Println()
}








Ada: with Ada.Text_IO;

procedure Attractive_Numbers is

   function Is_Prime (N : in Natural) return Boolean is
      D : Natural := 5;
   begin
      if N < 2       then  return False;  end if;
      if N mod 2 = 0 then  return N = 2;  end if;
      if N mod 3 = 0 then  return N = 3;  end if;

      while D * D <= N loop
         if N mod D = 0 then  return False;  end if;
         D := D + 2;
         if N mod D = 0 then  return False;  end if;
         D := D + 4;
      end loop;
      return True;
   end Is_Prime;

   function Count_Prime_Factors (N : in Natural) return Natural is
      NC    : Natural := N;
      Count : Natural := 0;
      F     : Natural := 2;
   begin
      if NC = 1        then  return 0;  end if;
      if Is_Prime (NC) then  return 1;  end if;
      loop
         if NC mod F = 0 then
            Count := Count + 1;
            NC := NC / F;

            if NC = 1 then
               return Count;
            end if;

            if Is_Prime (NC) then F := NC; end if;
         elsif F >= 3 then
            F := F + 2;
         else
            F := 3;
         end if;
      end loop;
   end Count_Prime_Factors;

   procedure Show_Attractive (Max : in Natural)
   is
      use Ada.Text_IO;
      package Integer_IO is
         new Ada.Text_IO.Integer_IO (Integer);
      N     : Natural;
      Count : Natural := 0;
   begin
      Put_Line ("The attractive numbers up to and including " & Max'Image & " are:");
      for I in 1 .. Max loop
         N := Count_Prime_Factors (I);
         if Is_Prime (N) then
            Integer_IO.Put (I, Width => 5);
            Count := Count + 1;
            if Count mod 20 = 0 then New_Line; end if;
         end if;
      end loop;
   end Show_Attractive;

begin
   Show_Attractive (Max => 120);
end Attractive_Numbers;