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 8631477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:13:29+00:00 2026-06-12T09:13:29+00:00

Consider the following method: public static boolean isPrime(int n) { return ! (new String(new

  • 0

Consider the following method:

public static boolean isPrime(int n) {
    return ! (new String(new char[n])).matches(".?|(..+?)\\1+");
}

I’ve never been a regular expression guru, so can anyone fully explain how this method actually works? Furthermore, is it efficient compared to other possible methods for determining whether an integer is prime?

  • 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-12T09:13:30+00:00Added an answer on June 12, 2026 at 9:13 am

    First, note that this regex applies to numbers represented in a unary counting system, i.e.

    1       is 1
    11      is 2
    111     is 3
    1111    is 4
    11111   is 5
    111111  is 6
    1111111 is 7
    

    and so on. Really, any character can be used (hence the .s in the expression), but I’ll use “1”.

    Second, note that this regex matches composite (non-prime) numbers; thus negation detects primality.


    Explanation:

    The first half of the expression,

    .?
    

    says that the strings “” (0) and “1” (1) are matches, i.e. not prime (by definition, though arguable.)

    The second half, in simple English, says:

    Match the shortest string whose length is at least 2, for example, “11” (2). Now, see if we can match the entire string by repeating it. Does “1111” (4) match? Does “111111” (6) match? Does “11111111” (8) match? And so on. If not, then try it again for the next shortest string, “111” (3). Etc.

    You can now see how, if the original string can’t be matched as a multiple of its substrings, then by definition, it’s prime!

    BTW, the non-greedy operator ? is what makes the “algorithm” start from the shortest and count up.


    Efficiency:

    It’s interesting, but certainly not efficient, by various arguments, some of which I’ll consolidate below:

    • As @TeddHopp notes, the well-known sieve-of-Eratosthenes approach would not bother to check multiples of integers such as 4, 6, and 9, having been “visited” already while checking multiples of 2 and 3. Alas, this regex approach exhaustively checks every smaller integer.

    • As @PetarMinchev notes, we can “short-circuit” the multiples-checking scheme once we reach the square root of the number. We should be able to because a factor greater than the square root must partner with a factor lesser than the square root (since otherwise two factors greater than the square root would produce a product greater than the number), and if this greater factor exists, then we should have already encountered (and thus, matched) the lesser factor.

    • As @Jesper and @Brian note with concision, from a non-algorithmic perspective, consider how a regular expression would begin by allocating memory to store the string, e.g. char[9000] for 9000. Well, that was easy, wasn’t it? 😉

    • As @Foon notes, there exist probabilistic methods which may be more efficient for larger numbers, though they may not always be correct (turning up pseudoprimes instead). But also there are deterministic tests that are 100% accurate and far more efficient than sieve-based methods. Wolfram’s has a nice summary.

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

Sidebar

Related Questions

Consider the following method in Java: public static boolean expensiveComputation() { for (int i
Consider the following method example: public static string[] ParseOptions() { return Environment.GetCommandLineArgs(); } What
Consider the following Java method: public static void f(int n) { if (n<=1) {
Consider the following code public static void method(String[] srgs){ try{ }catch(){ System.out.println(Hello World +
Consider the following method signature: public static bool TryGetPolls(out List<Poll> polls, out string errorMessage)
Consider the following method, where I build a string and return it. I would
Consider the following code :- public class UsingWait1{ public static void main(String... aaa){ CalculateSeries
Consider the following snippet: public class ReflectionTest { public static void main(String[] args) {
Consider the following class public class PlanetKrypton { public static void CallSuperManforHelp(string helpMessage, params
Consider the following C# code: class Program { static public void Print(string toPrint) {

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.