In: Computer Science
Problem: Given an integer k, find the two closest integers in absolute difference and the whole product equals k+1 or k+2. Return the two integers in any order. Write the code in java.
You can assume the integer value will be between [1,10^9]
You can multiple a divisor with itself.
Description of a sample run 1:
The input number 8 hence, k+1 is 9 and k+2 is 10
The divisors for 9 are: 1,3,9 and for 10: 1,2,5,10
For both cases, we have 2 divisors that multiply exactly to the values (3*3=15 and 2*5=10), we also have 1*9 and 1*10, however, this will yield the largest difference
However, we want to pick the product with the lowest absolute value hence: 3-3=0 and 5-2=3 so our correct choice is 3,3
Sample Input:
Input: num=8
Output: [3,3]
import java.io.*;
import java.util.*;
import java.util.Scanner;
import java.lang.Math;
class Factors{
    public static List<List<Integer> >
factComb(int n)
    {
       
List<List<Integer> > r_list =
                    
new ArrayList<List<Integer> >();
        List<Integer> list
= new ArrayList<Integer>();
        factorsListFunc(2, 1, n,
r_list, list);
        return r_list;
    }
    public static void factorsListFunc(int first,
int each_prod, int n,
    List<List<Integer> > r_list,
List<Integer> s_r_list)
    {
        if (first > n ||
each_prod > n)
           
return;
        if (each_prod == n)
{
           
ArrayList<Integer> t =new
ArrayList<Integer>(s_r_list);
           
r_list.add(t);
           
return;
        }
        for (int i = first; i
< n; i++) {
           
if (i * each_prod > n)
               
break;
           
if (n % i == 0) {
               
s_r_list.add(i);
               
factorsListFunc(i, i * each_prod, n, r_list, s_r_list);
               
s_r_list.remove(s_r_list.size() - 1);
           
}
        }
    }
    static void printDivisors(int n)
    {
        for (int
i=1;i<=n;i++)
           
if (n%i==0)
               
System.out.printf("%d ",i);
    }
    public static void main(String[] args)
    {
        //Scanner value = new
Scanner(System.in);
   // System.out.println("Enter integer value: ");
    int k = 8; //value.nextInt();
    System.out.println("value of k is: " + k);
    int a=k+1, b=k+2;
    System.out.println("k+1 is: " + a);
    System.out.println("k+2 is: " + b);
        System.out.println("The
divisors of k+1 are: ");
        printDivisors(a);
       
System.out.println("\nThe divisors of k+2 are: ");
        printDivisors(b);
       
List<List<Integer> > resultant = factComb(a);
        
List<List<Integer> > resultant1 = factComb(b);
       System.out.println("\ndivisors
that multiply exactly to the value:");
       int m=0, d=0,c=0;
         for
(List<Integer> i : resultant1) {
           
for (int j : i)
              
if(i.size()==2)
                 
System.out.print(j + " ");
           
System.out.println();
        }
        for (List<Integer>
i : resultant) {
           
for (int j : i)
              
if(i.size()==2)
                 
System.out.print(j + " ");
           
System.out.println();
        }
        for (List<Integer>
i : resultant) {
           
for (int j : i)
              
if(i.size()==2)
              
d=Math.abs(i.get(0)-i.get(1));
              
if(m<=d)
              
m=d;
         }
           for
(List<Integer> i : resultant1) {
           
for (int j : i)
              
if(i.size()==2)
             
{ c=Math.abs(i.get(0)-i.get(1)); }
              
         }
         if(c<m)
          for
(List<Integer> i : resultant1) {
           
for (int j : i)
              
if(i.size()==2)
             
{    d=Math.abs(i.get(0)-i.get(1));
                 
System.out.print(j + " "); }
                  
c=Math.max(m,d);
           
System.out.println("absolute value is: " + c);
           
System.out.println();
        }
         else
          for
(List<Integer> i : resultant) {
           
for (int j : i)
              
if(i.size()==2)
             
{ d=Math.abs(i.get(0)-i.get(1));
                 
System.out.print(j + " "); }
                
c=Math.min(m,d);
           
System.out.println("is the output with absolute value : " +
c);
           
System.out.println();
        }
    }  
}