Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9206083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:06:03+00:00 2026-06-18T00:06:03+00:00

Is this the most elegant way to express a isPrime function for BigInt objects?

  • 0

Is this the most elegant way to express a isPrime function for BigInt objects?

Here’s what I have for regular integers:

  def isPrimeForInt(n: Int): Boolean = {
    val ceiling = math.sqrt(n.toDouble).toInt
    (2 until ceiling) forall (x => n % x != 0)
  }

Here’s what I have for BigInts:

  def isPrimeForBigInt(n: BigInt): Boolean = {
    def ceiling: BigInt = {
      def f(a: BigInt): Stream[BigInt] = a #:: f(a+1)
      f(BigInt(1)).dropWhile(_.pow(2) < n)(0)
    }
    Range.BigInt(BigInt(2), ceiling , BigInt(1)) forall (x => n % x != 0)
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:06:04+00:00Added an answer on June 18, 2026 at 12:06 am

    Here is my primality checker for BigInts:

    private static Boolean isSpsp(BigInteger n, BigInteger a)
    {
        BigInteger two = BigInteger.valueOf(2);
        BigInteger n1 = n.subtract(BigInteger.ONE);
        BigInteger d = n1;
        int s = 0;
    
        while (d.mod(two).equals(BigInteger.ZERO))
        {
            d = d.divide(two);
            s += 1;
        }
    
        BigInteger t = a.modPow(d, n);
    
        if (t.equals(BigInteger.ONE) || t.equals(n1))
        {
            return true;
        }
    
        while (--s > 0)
        {
            t = t.multiply(t).mod(n);
            if (t.equals(n1))
            {
                return true;
            }
        }
    
        return false;
    }
    
    public static Boolean isPrime(BigInteger n)
    {
        Random r = new Random();
        BigInteger two = BigInteger.valueOf(2);
        BigInteger n3 = n.subtract(BigInteger.valueOf(3));
        BigInteger a;
        int k = 25;
    
        if (n.compareTo(two) < 0)
        {
            return false;
        }
    
        if (n.mod(two).equals(BigInteger.ZERO))
        {
            return n.equals(two);
        }
    
        while (k > 0)
        {
            a = new BigInteger(n.bitLength(), r).add(two);
            while (a.compareTo(n) >= 0)
            {
                a = new BigInteger(n.bitLength(), r).add(two);
            }
    
            if (! isSpsp(n, a))
            {
                return false;
            }
    
            k -= 1;
        }
    
        return true;
    }
    

    You can read more about it at my Programming with Prime Numbers essay.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the most elegant way to implement this function: ArrayList generatePrimes(int n) This
Do you have any insights into the most elegant way of persisting objects from
What is the most elegant way to vectorize a scala function that returns multiple
What's the most elegant way of doing something like this: >>> tests = [false,
I'm looking for the most elegant way to parse this. I'm just hitting a
What is the most elegant way to say this in JS?: search_keyword = search_keyword.toLowerCase();
What's the most elegant way to get this array [10, 20, 30, 40, 50]
Here's another what's the most elegant way to do X in Ruby type of
What is the easiest/most elegant way to do the following in python: def piecewiseProperty(aList):
In C#, what's the most elegant way to create an array of objects, from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.