Problem:
For the integers in the given range find the number that has the most divisors.
Solution:
package com.myprograms;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class MaximumDivisors {
int lowerLimit;
int upperLimit;
List<Integer> divisors = new ArrayList<Integer>();
List<Integer> resultList = new ArrayList<Integer>();
int result;
public static void main(String[] args) {
MaximumDivisors maximumDivisors = new MaximumDivisors();
maximumDivisors.getTheLimits();
maximumDivisors.findTheMaximumDivisors();
maximumDivisors.printTheResult();
}
public void getTheLimits(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter lower limit value");
lowerLimit = scanner.nextInt();
System.out.println("enter upper limit value");
upperLimit = scanner.nextInt();
scanner.close();
}
public void findTheMaximumDivisors(){
for(int i = lowerLimit; i<= upperLimit; i++){
resultList.add(findExactDivisors(i));
}
Collections.sort(resultList);
result = resultList.get(resultList.size() - 1);
}
public int findExactDivisors(int n){
divisors = new ArrayList<Integer>();
for(int i = 1; i<=n; i++ ){
if(n%i == 0){
divisors.add(i);
}
}
return divisors.size();
}
public void printTheResult(){
System.out.println("the maximum number of divisors in the set " + lowerLimit + " " + upperLimit +" is: " + result);
}
}
Output:
enter lower limit value
1
enter upper limit value
10
the maximum number of divisors in the set 1 10 is: 4
For the integers in the given range find the number that has the most divisors.
Solution:
package com.myprograms;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class MaximumDivisors {
int lowerLimit;
int upperLimit;
List<Integer> divisors = new ArrayList<Integer>();
List<Integer> resultList = new ArrayList<Integer>();
int result;
public static void main(String[] args) {
MaximumDivisors maximumDivisors = new MaximumDivisors();
maximumDivisors.getTheLimits();
maximumDivisors.findTheMaximumDivisors();
maximumDivisors.printTheResult();
}
public void getTheLimits(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter lower limit value");
lowerLimit = scanner.nextInt();
System.out.println("enter upper limit value");
upperLimit = scanner.nextInt();
scanner.close();
}
public void findTheMaximumDivisors(){
for(int i = lowerLimit; i<= upperLimit; i++){
resultList.add(findExactDivisors(i));
}
Collections.sort(resultList);
result = resultList.get(resultList.size() - 1);
}
public int findExactDivisors(int n){
divisors = new ArrayList<Integer>();
for(int i = 1; i<=n; i++ ){
if(n%i == 0){
divisors.add(i);
}
}
return divisors.size();
}
public void printTheResult(){
System.out.println("the maximum number of divisors in the set " + lowerLimit + " " + upperLimit +" is: " + result);
}
}
enter lower limit value
1
enter upper limit value
10
the maximum number of divisors in the set 1 10 is: 4
enter lower limit value
1
enter upper limit value
100
the maximum number of divisors in the set 1 100 is: 12
enter lower limit value
10
enter upper limit value
48
the maximum number of divisors in the set 10 48 is: 10
No comments:
Post a Comment