Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Number System and Arithmetic
  • Algebra
  • Set Theory
  • Probability
  • Statistics
  • Geometry
  • Calculus
  • Logarithms
  • Mensuration
  • Matrices
  • Trigonometry
  • Mathematics
Open In App
Next Article:
Prime Number Formulas
Next article icon

Prime Number Formulas

Last Updated : 15 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Prime numbers are defined as numbers greater than 1 that have no divisors other than 1 and themselves. These unique numbers play a crucial role in various fields, including cryptography, number theory, and computer science.

There are several methods to identify prime numbers, and formulas like the well-known 6n ± 1 and n² + n + 41 help generate primes under specific conditions. This article will explore the different prime number formulas, methods for testing primality, and solved and practice problems of primes.

Table of Content

  • Prime Number Formulas
  • 6n ± 1 Formula
  • n2 + n + 41 Formula
  • How to Check if a Number is Prime?
    • Trial Division Method

Prime Number Formulas

Prime number formulas are mathematical expressions that help identify or generate prime numbers. Here are a few commonly known formulas:

  • 6n ± 1 Formula
  • n2 + n + 41 Formula

6n ± 1 Formula

6n ± 1 formula is a way to identify potential prime numbers. It suggests that, for any integer n ≥ 1, prime numbers (except 2 and 3) can be expressed as either 6n+1 or 6n−1. This formula helps narrow down the possible prime candidates, as all primes greater than 3 follow this form.

Why 6n ± 1?

Most numbers divisible by 2 or 3 do not fit into the 6n ± 1 form. For example, numbers like 6n (divisible by 6) or 6n ± 2 (even numbers) are not prime, except for 2.

For Example:

    • For n = 1
      6 × 1 + 1 = 7 and 6 × 1 − 1 = 5. Both 5 and 7 are prime numbers.
    • For n = 2
      6 × 2 + 1 = 13 and 6 × 2 − 1 = 11, both of which are also prime​.
    • For n = 3
      6 × 3 + 1 = 19 and 6 × 3 − 1 = 17, both 17 and 19 are prime numbers.
    • For n = 4
      6 × 4 + 1 = 25 and 6 × 4 − 1 = 23, 23 is prime, but 25 is not.
    • For n = 5
      6 × 5 + 1 = 31 and 6 × 5 − 1 = 29, both 29 and 31 are prime numbers.
    • For n = 6
      6 × 6 + 1 = 37 and 6 × 6 − 1 = 35, 37 is prime, but 35 is not.
    • For n = 7
      6 × 7 + 1 = 43 and 6 × 7 − 1 = 41, both 41 and 43 are prime numbers.

    n2 + n + 41 Formula

    n2 + n + 41 formula is a famous quadratic formula that generates prime numbers for values of n between 0 and 39. It was discovered by the mathematician Euler, and it demonstrates an interesting property where consecutive values of n produce prime numbers. However, beyond n = 39, the formula starts to generate composite numbers.

    For n = 0 to n = 39, the output of this formula results in a prime number. For example:

      • When n = 0
        02 + 0 + 41 = 41 (Prime No.)
      • When n = 1
        12 + 1 + 41 = 43 (Prime No.)
      • When n = 2
        22 + 2 + 41 = 47 (Prime No.)
      • When n = 3
        32 + 3 + 41 = 53 (Prime No.)
      • When n = 4
        42 + 4 + 41 = 61 (Prime No.)
      • When n = 5
        52 + 5 + 41 = 71 (Prime No.)
      • When n = 6
        62 + 6 + 41 = 83 (Prime No.)
      • When n = 7
        72 + 7 + 41 = 97 (Prime No.)

      However, for values of n ≥ 40, this formula begins to produce non-prime numbers. For instance:

      • When n = 40, 402 + 40 + 41 = 1681, which is not prime (it’s 412)

      How to Check if a Number is Prime?

      To check if a number is prime, several methods can be used depending on the size of the number:

      Trial Division Method

      This is the simplest way to check if a number is prime. It involves dividing the number by all integers starting from 2 up to the square root of the number.

      • If the number is divisible by any of these integers, it is not a prime number.
      • If none of these integers divide the number, then it is a prime number.
      • Example: To check if 29 is prime, check divisibility by all integers from 2 to √29​ (approximately 5.39). Since 29 is not divisible by 2, 3, or 5, it is a prime number.

      Note: We can also use the discussed formulas for the same.

      Solved Examples of Prime Number Formula

      Example 1: Check if 29 is a prime number.

      Solution:

      Step 1: Use the formula 6n + 1 or 6n − 1 to find if 29 fits.

      Step 2: For n = 5, 6 × 5 − 1 = 29.

      Step 3: Since 29 fits the 6n − 1 form, it could be prime.

      Step 4: Now, check divisibility by smaller primes (2, 3, 5). Since 29 is not divisible by any of these, it is confirmed to be a prime number​.

      Example 2: Find the prime numbers generated by the formula for n = 0 to n = 3.

      Solution:

      For n = 0, 02 + 0 + 41 = 41, which is prime.

      For n = 1, 12 + 1 + 41 = 43, which is prime.

      For n = 2, 22 + 2 + 41 = 47, which is prime.

      For n = 3, 32 + 3 + 41 = 53, which is prime

      Example 3: Check if 77 is Prime or not Using Trial Division.

      Solution:

      Step 1: Find the square root of 77.

      The approximate square root of 77 is ≈ 8.77. This means we only need to check divisibility by the prime numbers less than or equal to 8 (i.e., 2, 3, 5, and 7).

      Step 2: Check divisibility by 2.

      77 is odd, so it is not divisible by 2.

      Step 3: Check divisibility by 3.

      The sum of the digits of 77 is 7+7=147 + 7 = 147+7=14, which is not divisible by 3. Therefore, 77 is not divisible by 3.

      Step 4: Check divisibility by 5.

      Since 77 does not end in 0 or 5, it is not divisible by 5.

      Step 5: Check divisibility by 7.

      77 ÷ 7 = 11. Since 77 is divisible by 7, it has a divisor other than 1 and itself.

      So 77 is not a prime number, as it has divisors other than 1 and itself​

      Practice Problems on Prime Number Formulas

      Problem 1: Using the 6n±1 formula, determine whether the following numbers are prime: 17, 23, 31, 41.

      Problem 2: Apply the quadratic formula n2+n+41 for values of n = 4, 5, 6, 7 and check if the resulting numbers are prime.

      Problem 3: Find all prime numbers between 50 and 100 using the 6n ± 1 formula. Verify your results.

      Problem 4: Test whether 83 is a prime number using the trial division method.

      Read More,

      • Prime Numbers
      • Prime Factorization
      • Composite Numbers

      Next Article
      Prime Number Formulas

      S

      somesh_barthwal
      Improve
      Article Tags :
      • Mathematics
      • School Learning
      • Prime Number
      • Maths
      Practice Tags :
      • Prime Number

      Similar Reads

        Prime Number Chart
        Prime Number Chart is a helpful tool used to display and identify prime numbers. Prime Numbers are natural numbers greater than 1 that can only be divided by 1 and the number itself. Unlike composite numbers, which can be divided by other numbers, primes are unique because of their indivisibility. F
        4 min read
        What is a Prime Number?
        Prime numbers are unique numbers that can only be divided evenly by 1 and themselves. This means they have exactly two distinct positive divisor. A prime number is always greater than 1 and has no other factors besides 1 and itself. For instance, 13 is a prime number because it can only be divided b
        2 min read
        Prime and Composite Numbers
        Prime and Composite Numbers are commonly used classifications of Natural Numbers based on divisibility. A prime number is divisible by only 1 and itself and a composite number is divisible by one or two more other Every natural number, except 0 or 1, is either prime or composite. Table of ContentPri
        5 min read
        How to Find Prime Numbers?
        Prime Numbers are those numbers that have only two factors: 1 and the number itself. In other words, a prime number is that number that is exactly divisible by 1 and the number itself. A Prime number should contain exactly two factors.A prime number should be divisible by 1 and the number itself.Let
        8 min read
        Euler's Prime Formula
        Euler's prime formula is an interesting expression that generates prime numbers for consecutive integer values of n, though only up to a certain point. This formula is discovered by the brilliant Swiss mathematician Leonhard Euler, is one of the earliest examples of number-theoretic patterns related
        3 min read
      geeksforgeeks-footer-logo
      Corporate & Communications Address:
      A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
      Registered Address:
      K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
      GFG App on Play Store GFG App on App Store
      Advertise with us
      • Company
      • About Us
      • Legal
      • Privacy Policy
      • In Media
      • Contact Us
      • Advertise with us
      • GFG Corporate Solution
      • Placement Training Program
      • Languages
      • Python
      • Java
      • C++
      • PHP
      • GoLang
      • SQL
      • R Language
      • Android Tutorial
      • Tutorials Archive
      • DSA
      • Data Structures
      • Algorithms
      • DSA for Beginners
      • Basic DSA Problems
      • DSA Roadmap
      • Top 100 DSA Interview Problems
      • DSA Roadmap by Sandeep Jain
      • All Cheat Sheets
      • Data Science & ML
      • Data Science With Python
      • Data Science For Beginner
      • Machine Learning
      • ML Maths
      • Data Visualisation
      • Pandas
      • NumPy
      • NLP
      • Deep Learning
      • Web Technologies
      • HTML
      • CSS
      • JavaScript
      • TypeScript
      • ReactJS
      • NextJS
      • Bootstrap
      • Web Design
      • Python Tutorial
      • Python Programming Examples
      • Python Projects
      • Python Tkinter
      • Python Web Scraping
      • OpenCV Tutorial
      • Python Interview Question
      • Django
      • Computer Science
      • Operating Systems
      • Computer Network
      • Database Management System
      • Software Engineering
      • Digital Logic Design
      • Engineering Maths
      • Software Development
      • Software Testing
      • DevOps
      • Git
      • Linux
      • AWS
      • Docker
      • Kubernetes
      • Azure
      • GCP
      • DevOps Roadmap
      • System Design
      • High Level Design
      • Low Level Design
      • UML Diagrams
      • Interview Guide
      • Design Patterns
      • OOAD
      • System Design Bootcamp
      • Interview Questions
      • Inteview Preparation
      • Competitive Programming
      • Top DS or Algo for CP
      • Company-Wise Recruitment Process
      • Company-Wise Preparation
      • Aptitude Preparation
      • Puzzles
      • School Subjects
      • Mathematics
      • Physics
      • Chemistry
      • Biology
      • Social Science
      • English Grammar
      • Commerce
      • World GK
      • GeeksforGeeks Videos
      • DSA
      • Python
      • Java
      • C++
      • Web Development
      • Data Science
      • CS Subjects
      @GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
      We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
      Lightbox
      Improvement
      Suggest Changes
      Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
      geeksforgeeks-suggest-icon
      Create Improvement
      Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
      geeksforgeeks-improvement-icon
      Suggest Changes
      min 4 words, max Words Limit:1000

      Thank You!

      Your suggestions are valuable to us.

      What kind of Experience do you want to share?

      Interview Experiences
      Admission Experiences
      Career Journeys
      Work Experiences
      Campus Experiences
      Competitive Exam Experiences