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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:33:57+00:00 2026-06-18T08:33:57+00:00

I know it’s been discussed many times; I’ve read it, but somehow I can’t

  • 0

I know it’s been discussed many times; I’ve read it, but somehow I can’t get it.
I want to write a program that determines if the entered number is prime or not.

One of the implementations I found somewhere on the Internet:

from math import *

def main():
    n = abs(input("Enter a number: "))
    i = 2
    msg = 'is a prime number.'
    while i <= sqrt(n):
        if n % i == 0:
            msg = 'is not a prime number.'
        i = i + 1
    print n, msg


main()

A couple of questions here:

  • In the above, what is i, and why does it have a starting value of 2?
  • What does i = i + 1 do in this program?
  • How does the interpreter know when to print 'is a prime number.' even though it is out of the body loop?
  • 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-18T08:33:59+00:00Added an answer on June 18, 2026 at 8:33 am

    A prime number is a number that’s only divisible by 1 and itself. The method it’s using is to try dividing your candidate number n by every other number from 2 up to itself; however if any number i is a divisor of your number n then so is n / i and at least one of them is less than or equal to sqrt(n) therefore we need only test up to sqrt(n) inclusive. In practice we need only test the divisors that are actually prime themselves but since we don’t have a list of primes to hand we’ll test every one.

    what in the above i is? and why it got a 2 starting value?

    i is the potential factor of n we’re testing. It starts with 2 because we don’t care if 1 divides n (and trivially it will) because the prime definition allows / expects that.

    what is the i = i + 1 statement, in this concrete example for? Can’t see its use in the program.

    It’s incrementing the i value at the end of the loop defined by the while i <= sqrt(n); it means we advance i to test the next candidate divisor of n.

    and finally, how python knows when to print ‘is a prime number.’ although it is out of the body loop?

    We initialise msg to “is a prime number” and if we find any divisor then we change it to “is not a prime number” inside the loop. If the loop doesn’t find a divisor, or if the loop never runs, we’ll use the initial value we set which is “is a prime number”. Incidentally you could break out of the loop when you find a divisor; there’s no point carrying on the test after that.

    As another aside you probably want to compute sqrt(n) outside the while and store than in a variable to use in the while – you may be recalculating the square root for every iteration, which is relatively expensive.

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

Sidebar

Related Questions

I know that this has been discussed thousand times but I still cannot figure
I know that this sort of question has been asked here before, but still
I know that there has been one question about this but it is not
I know that you can use DirectoryIndex example.html in .htaccess, but what I need
I know this has been asked a million times, but I haven't had much
I know that I can internally link with canvas, but my whole doc's set
Know this might be rather basic, but I been trying to figure out how
I know you can not set a key value dynamically, but what about the
I know that Phonegap has an event for back button, but it's only available
I know there have been many questions on grid and pack in the past

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.