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
pratiksha tamrakarpratiksha tamrakar 

Apex Code to find the number whether it is Prime or not

Hi ,
I am new to salesforce and trying some basic code. I was trying to check the number whether the number is prime or not , But not getting idea of code . Can anyone please help me.
SwethaSwetha (Salesforce Developers) 
HI Pratiksha,
If you want to check if integer number is prime, try below
public static Boolean isPrime(Integer n) {
    //check if n is a multiple of 2
    if (n != 2 && Math.mod(n, 2) == 0) return false;
    //if not, then just check the odds
    for(Integer i=3; i*i<=n; i+=2) {
        if(Math.mod(n, i) == 0)
            return false;
    }
    return true;
}

Reference:https://salesforce.stackexchange.com/questions/180681/kindly-check-is-this-code-is-fine/180688

Also, see comments posted on https://developer.salesforce.com/forums/?id=9060G0000005mSEQAY 

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you