function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fiona gentryfiona gentry 

How can I see exactly three positive divisors using Collections

Hi Folks,

 I have a problem where for Given an integer n, i want to return true if n has exactly three positive divisors. Otherwise, return false.

Example 1:

Input: n = 2
Output: false
Explantion: 2 has only two divisors: 1 and 2.

Example 2:

Input: n = 4
Output: true
Explantion: 4 has three divisors: 1, 2, and 4.

Tried to use List to achieve ,but no success,can any one solve the code below or it needs a Map?
public class ThreeDivisors {
    
    public static List<Integer> returnThreeDivisors(Integer input){
        
        List<Integer> inputList = new List<Integer>();
        Integer divisor = 0;
        
        for (Integer i=0; i<inputList.size(); i++) {
                    Integer var = inputList[i];
                    
                    if ( Math.mod(var,i) == 0)
                    {
                        divisor++;
                    }
                }
        
        return inputList;
    }

}

Regards,
Fiona
 
Best Answer chosen by fiona gentry
mukesh guptamukesh gupta
Hi Flona,

Please use below code:-
 
public class ThreeDivisors {
    
    public static List<Integer> returnThreeDivisors(Integer input){
List<Integer> inputList = new List<Integer>();
        
        for (Integer i=1; i<input+1; i++) {
                    if ( Math.mod(input,i) == 0)
                    {
                       inputList.add(i)
                        
                    }
                }
        
        return inputList;
}
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

All Answers

mukesh guptamukesh gupta
Hi Flona,

Please use below code:-
 
public class ThreeDivisors {
    
    public static List<Integer> returnThreeDivisors(Integer input){
List<Integer> inputList = new List<Integer>();
        
        for (Integer i=1; i<input+1; i++) {
                    if ( Math.mod(input,i) == 0)
                    {
                       inputList.add(i)
                        
                    }
                }
        
        return inputList;
}
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
This was selected as the best answer
AbhinavAbhinav (Salesforce Developers) 
Hi Fiona,

Not sure why you have used returntype for your method as List<Integer>  as for your use case you want to return true or false ideally I think it should be Boolean.

You have used inputList.size()  in for loop however inputList  size is zero as you have not populated it.

Base on your usecase you can try below code
 
public class ThreeDivisors {
    
    public static Boolean returnThreeDivisors(Integer input){
        
        
        Integer divisor = 0;
        
        for (Integer i=1; i<=input; i++) {
                   
                    if ( Math.mod(Input,i) == 0)
                    {
                        divisor++;
                    }
                }
        
        If(divisor== 3)
        {
            return true;
        }
        else 
            return false;
        
            }

}

Let me know if you are tring to acheive any other scenario.I have edited code based on use case.

If it helps mark it as best answer.

Thanks!