/*Lab 6 for 11th September 2008*/ /*Divisor summatory function : A divisor function is an arithmetical function related to the divisors of an integer. It is defined as the number of divisors of an integer. e.g. Divisor function of 12, σ0 (12) = 6, since the divisors of 12 are: 1, 2, 3, 4, 6, 12. ( For more about divisor function, visit http: //en.wikipedia.org/wiki/Divisor_function ) The Divisor summatory function of n is defined as the sum of the divisor function of the first n positive integers. It is given by the following formula: n D(n) = σ0 (k) k=1 e.g. D(6) = σ0 (1) + σ0 (2) + σ0 (3) + σ0 (4) + σ0 (5) + σ0 (6) = 1+2+2+3+2+4 = 14 Write a JAVA program to find D(n) for any positive integer n passed as a command line argument. Your program should implement and use the following function: (int) divisorFunction (int k): Returns σ0 (k). */ /* key idea: create a static function int divisorFunction(int k) which counts the number of divisors. then using a for loop get the number of divisors for each number by calling the functin and sum them up.*/ class DivisorSummatory { public static int divisorFunction(int k) // calculates the number of divisors of a number*/ { int count=0; for(int i=1;i<=k;i++) { if(k%i == 0) count++; } return count; } public static void main(String args[]) { int num,sum; num = Integer.parseInt(args[0]); sum = 0; for(int i=1;i<=num;i++) { sum += divisorFunction(i); System.out.println(i + " divisor " + divisorFunction(i)); } System.out.println(sum); } }