프로그램 언어 배우는 과정의 한 예제로는 적당하지만, 알고리즘 면으로는 매우 비효율적인 소스를 C, C++, Java, C#, Python, Ruby, Groovy, Lua, Octave 언어 등으로 작성해 보았다.



* 모든 양의 약수를 구하고 소수인지 아닌지 판별하는 C 언어 소스

/**
 * Filename: findAllDivisorsNotGoodC_01.c
 *
 *  Purpose:
 *          Find all positive divisors of a given nonzero integer.
 *
 *  With VC++
 *      Compile: cl findAllDivisorsNotGoodC_01.c
 *      Execute: findAllDivisorsNotGoodC_01
 *
 *  With gcc
 *      Compile: gcc findAllDivisorsNotGoodC_01.c -o findAllDivisorsNotGoodC_01
 *      Execute: ./findAllDivisorsNotGoodC_01
 *
 *  Date: 2011. 11. 1 (Tue)
 */

#include <stdio.h>
#include <math.h>

void printUsage(const char cmd[]) {
 printf("Usage: %s [nnn]\n", cmd);
 printf("nnn should be a positive integer.\n");
}

int main(int argc, char *argv[]) {
 int n;
 int cnt = 0;
 long sum = 0;
 int i;
 if (argc == 2) {
  n = atoi(argv[1]);
  printf("The input number is %d.\n", n);
  if (n > 0) {
   if (n <= 0x3FFFFFFF) {
    printf("The positive divisors of %d are ", n);
    for (i = 1; i <= n; i++) {
     if (n % i == 0) {
      printf("%d", i);
      cnt++;
      sum += i;
      if (i < n) {
       printf(", ");
      }
     }
    }
    printf(".\n");
    printf("The sum of all positive divisors of %d is %ld.\n", n, sum);
    printf("The number of positive divisors of %d is %d.\n", n, cnt);
    if (cnt == 2) {
     printf("Hence the positive integer %d is a prime number.\n", n);
    }
    else {
     printf("Hence the positive integer %d is not a prime number.\n", n);
    }
   }
   else {
    printf("It is too big.\n");
    printf("The input integer shoud be in the range from 1 to %d.\n", 0x3FFFFFFF);
   }
  }
  else {
   printf("It is not positive.\n");
  }
 }
 else {
  printUsage(argv[0]);
 }
    return 0;
}




* 모든 양의 약수를 구하고 소수인지 아닌지 판별하는 C++ 언어 소스

/**
 * Filename: findAllDivisorsNotGoodCPP_01.cpp
 *
 *  Purpose:
 *          Find all positive divisors of a given nonzero integer.
 *
 *  With VC++
 *      Compile: cl findAllDivisorsNotGoodCPP_01.cpp /EHsc
 *      Execute: findAllDivisorsNotGoodCPP_01
 *
 *  With g++
 *      Compile: g++ findAllDivisorsNotGoodCPP_01.cpp -o findAllDivisorsNotGoodCPP_01
 *      Execute: ./findAllDivisorsNotGoodCPP_01
 *
 *  Date: 2011. 11. 1 (Tue)
 */

#include <iostream>
#include <cmath>
#include <cstring>

using namespace std;

void printUsage(const char cmd[]) {
 cout << "Usage: " << cmd << " [nnn]" << endl;
 cout << "nnn should be a positive integer." << endl;
}

int main(int argc, char *argv[]) {
 int n;
 int cnt = 0;
 long sum = 0;
 int i;
 if (argc == 2) {
  n = atoi(argv[1]);
  cout << "The input number is " << n << "." << endl;
  if (n > 0) {
   if (n <= 0x3FFFFFFF) {
    cout << "The positive divisors of " << n << " are ";
    for (i = 1; i <= n; i++) {
     if (n % i == 0) {
      cout << i;
      cnt++;
      sum += i;
      if (i < n) {
       cout << ", ";
      }
     }
    }
    cout << "." << endl;
    cout << "The sum of all positive divisors of " << n << " is " << sum << "." << endl;
    cout << "The number of positive divisors of " << n << " is " << cnt << "." << endl;
    if (cnt == 2) {
     cout << "Hence the positive integer " << n << " is a prime number." << endl;
    }
    else {
     cout << "Hence the positive integer " << n << " is not a prime number." << endl;
    }
   }
   else {
    cout << "It is too big." << endl;
    cout << "The input integer shoud be in the range from 1 to " << 0x3FFFFFFF << "." << endl;
   }
  }
  else {
   cout << "It is not positive." << endl;
  }
 }
 else {
  printUsage(argv[0]);
 }
    return 0;
}




* 모든 양의 약수를 구하고 소수인지 아닌지 판별하는 C# 언어 소스

/**
 * Filename: FindAllDivisorsNotGoodCS_01.cs
 *
 *    Purpose:
 *          Find all positive divisors of a given nonzero integer.
 *
 *  Compile: csc FindAllDivisorsNotGoodCS_01.cs
 *  Execute: FindAllDivisorsNotGoodCS_01
 *
 *  Date: 2011. 11. 1 (Tue)
 */

using System;

public class FindAllDivisorsNotGoodCS_01 {

 static void PrintUsage(string cmd) {
  Console.WriteLine("Usage: " + cmd + " [nnn]");
  Console.WriteLine("nnn should be a positive integer.");
 }

 public static void Main(string[] args) {
  int n;
  int cnt = 0;
  long sum = 0;
  if (args.Length == 1) {
   try {
    n = Convert.ToInt32(args[0]);
    Console.WriteLine("The input number is " + n + ".");
    if (n > 0) {
     if (n <= 0x3FFFFFFF) {
      Console.Write("The positive divisors of " + n + " are ");
      for (int i = 1; i <= n; i++) {
       if (n % i == 0) {
        Console.Write("" + i);
        cnt++;
        sum += i;
        if (i < n) {
         Console.Write(", ");
        }
       }
      }
      Console.WriteLine(".");
      Console.WriteLine("The sum of all positive divisors of " + n + " is " + sum + ".");
      Console.WriteLine("The number of positive divisors of " + n + " is " + cnt + ".");
      if (cnt == 2) {
       Console.WriteLine("Hence the positive integer " + n + " is a prime number.");
      }
      else {
       Console.WriteLine("Hence the positive integer " + n + " is not a prime number.");
      }
     }
     else {
      Console.WriteLine("It is too big.");
      Console.WriteLine("The input integer shoud be in the range from 1 to " + 0x3FFFFFFF + ".");
     }
    }
    else {
     Console.WriteLine("It is not positive.");
    }
   }
   catch (FormatException ex) {
    Console.WriteLine(ex.Message + "\nTry agin with other number.");
    Console.WriteLine("The input integer shoud be in the range from 1 to " + 0x3FFFFFFF + ".");
    PrintUsage("FindAllDivisorsNotGoodCS_01");
   }
   catch (OverflowException ex) {
    Console.WriteLine(ex.Message + "\nTry agin with other number.");
    Console.WriteLine("The input integer shoud be in the range from 1 to " + 0x3FFFFFFF + ".");
    PrintUsage("FindAllDivisorsNotGoodCS_01");
   }
  }
  else {
   PrintUsage("FindAllDivisorsNotGoodCS_01");
  }
 }
}





* 모든 양의 약수를 구하고 소수인지 아닌지 판별하는 Java 언어 소스

/**
 * Filename: FindAllDivisorsNotGoodJava_01.java
 *
 *    Purpose:
 *          Find all positive divisors of a given nonzero integer.
 *
 *  Compile: javac FindAllDivisorsNotGoodJava_01.java
 *  Execute: java FindAllDivisorsNotGoodJava_01
 *
 *  Date: 2011. 11. 1 (Tue)
 */

public class FindAllDivisorsNotGoodJava_01 {

 static void printUsage(String cmd) {
  System.out.println("Usage: java " + cmd + " [nnn]");
  System.out.println("nnn should be a positive integer.");
 }

 public static void main(String[] args) {
  int n;
  int cnt = 0;
  long sum = 0;
  if (args.length == 1) {
   try {
    n = Integer.parseInt(args[0]);
    System.out.println("The input number is " + n + ".");
    if (n > 0) {
     if (n <= 0x3FFFFFFF) {
      System.out.print("The positive divisors of " + n + " are ");
      for (int i = 1; i <= n; i++) {
       if (n % i == 0) {
        System.out.print("" + i);
        cnt++;
        sum += i;
        if (i < n) {
         System.out.print(", ");
        }
       }
      }
      System.out.println(".");
      System.out.println("The sum of all positive divisors of " + n + " is " + sum + ".");
      System.out.println("The number of positive divisors of " + n + " is " + cnt + ".");
      if (cnt == 2) {
       System.out.println("Hence the positive integer " + n + " is a prime number.");
      }
      else {
       System.out.println("Hence the positive integer " + n + " is not a prime number.");
      }
     }
     else {
      System.out.println("It is too big.");
      System.out.println("The input integer shoud be in the range from 1 to " + 0x3FFFFFFF + ".");
     }
    }
    else {
     System.out.println("It is not positive.");
    }
   }
   catch (java.lang.NumberFormatException ex) {
    System.out.println(ex.getMessage() + " is not valid for input integers.\nTry agin with other number.");
    System.out.println("The input integer shoud be in the range from 1 to " + 0x3FFFFFFF + ".");
    printUsage("FindAllDivisorsNotGoodJava_01");
   }
  }
  else {
   printUsage("FindAllDivisorsNotGoodJava_01");
  }
 }
}



* 모든 양의 약수를 구하고 소수인지 아닌지 판별하는 Groovy 언어 소스

/**
 * Filename: findAllDivisorsNotGoodGroovy_01.groovy
 *
 *    Purpose:
 *          Find all positive divisors of a given nonzero integer.
 *
 *  Execute: groovy findAllDivisorsNotGoodGroovy_01.groovy 111141111
 *
 *  Compiling only: groovyc findAllDivisorsNotGoodGroovy_01.groovy
 *  Execute after compile: java -cp .;%GROOVY_HOME%\embeddable\groovy-all-1.8.2.jar findAllDivisorsNotGoodGroovy_01 111141111
 *
 *  Date: 2011. 11. 1 (Tue)
 */

def printUsage(cmd) {
 println("Usage: groovy " + cmd + " [nnn]")
 println("nnn should be a positive integer.")
}

def n
def cnt = 0
def sum = 0
if (args.length == 1) {
 try {
  n = args[0] as int;
  println("The input number is " + n + ".")
  if (n > 0) {
   if (n <= 0x3FFFFFFF) {
    print("The positive divisors of " + n + " are ")
    for (int i = 1; i <= n; i++) {
     if (n % i == 0) {
      print("" + i)
      cnt++
      sum += i
      if (i < n) {
       print(", ")
      }
     }
    }
    println(".")
    println("The sum of all positive divisors of " + n + " is " + sum + ".")
    println("The number of positive divisors of " + n + " is " + cnt + ".")
    if (cnt == 2) {
     println("Hence the positive integer " + n + " is a prime number.")
    }
    else {
     println("Hence the positive integer " + n + " is not a prime number.")
    }
   }
   else {
    println("It is too big.")
    println("The input integer shoud be in the range from 1 to " + 0x3FFFFFFF + ".")
   }
  }
  else {
   println("It is not positive.")
  }
 }
 catch (java.lang.NumberFormatException ex) {
  println(ex.getMessage() + " is not valid for input integers.\nTry agin with other number.")
  println("The input integer shoud be in the range from 1 to " + 0x3FFFFFFF + ".")
  printUsage("findAllDivisorsNotGoodGroovy_01.groovy")
 }
}
else {
 printUsage("findAllDivisorsNotGoodGroovy_01.groovy")
}



* 모든 양의 약수를 구하고 소수인지 아닌지 판별하는 Python 언어 소스

# Filename: findAllDivisorsNotGoodPython_01.py
#
#    Purpose:
#          Find all positive divisors of a given nonzero integer.
#
#  Execute: python findAllDivisorsNotGoodPython_01.py [nnn]
#
#  Date: 2011. 11. 1 (Tue)

import sys

def printUsage(cmd):
 print "Usage: python %s [nnn]" % cmd
 print "nnn should be a positive integer."

n = 0
cnt = 0
sum = 0

if len(sys.argv) == 2:
 try:
  pass
  n = int(sys.argv[1])
  print "The input number is %d." % n
  if n > 0:
   if n <= 0x3FFFFFFF:
    sys.stdout.write( "The positive divisors of %d are " % n )
    for i in range(1,  n + 1):
     if n % i == 0:
      sys.stdout.write("%d" % i)
      cnt += 1
      sum += i
      if i < n:
       sys.stdout.write(", ")
    print "."
    print "The sum of all positive divisors of %s is %s." % (n, sum)
    print "The number of positive divisors of %s is %s." % (n, cnt)
    if cnt == 2:
     print "Hence the positive integer %s is a prime number." % n
    else:
     print "Hence the positive integer %s is not a prime number." % n
   else:
    print "It is too big."
    print "The input integer shoud be in the range from 1 to %d." % 0x3FFFFFFF
  else:
   print "It is not positive."
 except ValueError, ex:
  print "%s.\nThe input string cannot be parsed as an integer.\nTry agin with other number." % ex
  print "The input integer shoud be in the range from 1 to %d." % 0x3FFFFFFF
  printUsage(sys.argv[0])
else:
 printUsage(sys.argv[0])




* 모든 양의 약수를 구하고 소수인지 아닌지 판별하는 Ruby 언어 소스

# Filename: findAllDivisorsNotGoodRuby_01.rb
#
#    Purpose:
#          Find all positive divisors of a given nonzero integer.
#
#  Execute: ruby findAllDivisorsNotGoodRuby_01.rb [nnn]
#
#
#  Date: 2011. 11. 1 (Tue)

def printUsage(cmd)
 print "Usage: ruby %s [nnn]\n" % cmd
 print "nnn should be a positive integer.\n"
end

n = 0
cnt = 0
sum = 0

if ARGV.length == 1
 begin
  n = ARGV[0].to_i
  print "The input number is %s.\n" % n
  if n > 0
   if n <= 0x3FFFFFFF
    print "The positive divisors of %d are " % n
    for i in 1..n do
     if n % i == 0
      print "%d" % i
      cnt += 1
      sum += i
      if i < n
       print ", "
      end
     end
    end
    print ".\n"
    print "The sum of all positive divisors of %s is %s.\n" % [n, sum]
    print "The number of positive divisors of %s is %s.\n" % [n, cnt]
    if cnt == 2
     print "Hence the positive integer %d is a prime number.\n" % n
    else
     print "Hence the positive integer %d is not a prime number.\n" % n
    end
   else
    print "It is too big.\n"
    print "The input integer shoud be in the range from 1 to %s\n." % 0x3FFFFFFF
   end
  else
   print "It is not positive.\n"
  end
 rescue SyntaxError, NameError => boom
  print "%s.\n" % boom
  print "The input string is not valid as an integer.\nTry agin with other number.\n" % boom
  print "The input integer shoud be in the range from 1 to %d.\n" % 0x3FFFFFFF
  printUsage("findAllDivisorsNotGoodGroovy_01.groovy")
 end
else
 printUsage("findAllDivisorsNotGoodRuby_01.rb")
end







* 모든 양의 약수를 구하고 소수인지 아닌지 판별하는 Lua 언어 소스

-- Filename: findAllDivisorsNotGoodLua_01.lua
--
--    Purpose:
--          Find all positive divisors of a given nonzero integer.
--
--  Execute: lua findAllDivisorsNotGoodLua_01.lua [nnn]
--
--
--  Date: 2011. 11. 1 (Tue)

function printUsage(cmd)
 print( "Usage: lua " .. cmd .. " [nnn]" )
 print "nnn should be a positive integer."
end

n = 0
cnt = 0
sum = 0

if #arg == 1 then
 if not tonumber(arg[1]) then
  print("Eception in parsing " .. arg[1] .. ".")
  print "Try agin with other number."
  print( "The input integer shoud be in the range from 1 to " .. 0x3FFFFFFF .. "." )
  printUsage(arg[0])
 else
  n = tonumber(arg[1])
  print( "The input number is " .. n .. "." )
  if n > 0 then
   if n <= 0x3FFFFFFF then
    io.write("The positive divisors of " .. n .. " are ")
    for i = 1,n do
     if n % i == 0 then
      io.write( "" .. i )
      cnt = cnt + 1
      sum = sum + i
      if i < n then
       io.write( ", " )
      end
     end
    end
    print ""
    print( "The sum of all positive divisors of " .. n .. " is " .. sum .. "." )
    print( "The number of positive divisors of " .. n .. " is " .. cnt .. "." )
    if cnt == 2 then
     print( "Hence the positive integer " .. n  .. " is a prime number." )
    else
     print( "Hence the positive integer " .. n  .. " is not a prime number." )
    end
   else
    print "It is too big."
    print( "The input integer shoud be in the range from 1 to " .. 0x3FFFFFFF .. "." )
   end
  else
   print "It is not positive.\n"
  end
 end
else
 printUsage(arg[0])
end




 

Posted by Scripter
,