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

  • Home
  • SEARCH
  • 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 6649521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:47:02+00:00 2026-05-26T00:47:02+00:00

Possible Duplicate: How to determine if a number is a prime with regex? Here

  • 0

Possible Duplicate:
How to determine if a number is a prime with regex?

Here is the code:

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

Can someone walk me through what the regex translates to in English and explain why it works?

  • 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-05-26T00:47:03+00:00Added an answer on May 26, 2026 at 12:47 am

    Recall what a prime number is and what it isn’t. First of all, this negates the regex match, so the regex matches on composite numbers.

    It first creates a string of length n, its characters are irrelevant as long as they are matched by ..

    That means the number is either 0 or 1 (which are not prime):

    .?
    

    or it must have two divisors greater than one:

    (..+?)\1+
    

    The first divisor is handled by the capturing group (..+?) which will match at least two characters (i.e. represent a number greater than or equal to two). The +? is a lazy quantifier so it will try to match as little as possible; this likely just speeds up the process.

    The \1 is a backreference matching the exact same thing the first group matched, this is repeated at least once using +. This repetition represents the second factor of the number to check. So if the group matches a characters and the \1+ repeats this b − 1 times you got yourself a representation of a ċ b. If this matches, n was a composite number and thus not prime.

    Regexes do backtracking in trying to create a match, so if it doesn’t work with \1 containing two characters it will try with three, four, etc. until either a match is found or the group captures more than half the string.

    So, e.g. for 14 the group would match two characters and \1 would then be repeated seven times, mimicking the factors of the number to test. Since this matches it has factors other than itself and one and thus isn’t prime.

    5 on the other hand would try with two characters in the group, then three and giving up there (since aaa cannot exist more than once in aaaaa). Thus five is prime.

    Here is a more thorough explanation, although once you figure it out mathematically it’s blindingly obvious and trivial.

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

Sidebar

Related Questions

Possible Duplicate: How to determine if a number is a prime with regex? This
Possible Duplicate: .NET: Determine the type of this class in its static method How
Possible Duplicates: C - determine if a number is prime Is there any way
Possible Duplicate: Determine if a type is static Duplicate of Determine if a type
Possible Duplicate: Fastest way to determine if an integer's square root is an integer
Possible Duplicate: Is there any way to determine text direction from CultureInfo in asp.net?
Possible Duplicate: Determine device (iPhone, iPod Touch) with iOS I am making a game
Possible Duplicate: How to determine if a linked list has a cycle using only
Possible Duplicate: How do I determine if a given date is Nth weekday of
Possible Duplicate: How can I determine if a geopoint is displayed in currently viewable

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.