- Notifications
You must be signed in to change notification settings - Fork629
Update P07_PrimeNumber.py#93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
VittalKumar23 wants to merge1 commit intoOmkarPathak:masterChoose a base branch fromVittalKumar23:patch-9
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Assuming the Number is Prime by Default:
The variable isPrime is initially set to True by default. This is a fundamental change from the original code, which assumed the number was not prime by default (isPrime = False). In primality testing, it's typically better to assume the number is prime until proven composite.
Input Validation:
The code checks if the input number number is greater than 1 before proceeding with prime checking. This step ensures that the input is a positive integer, as prime numbers are defined as greater than 1.
Iterating Through Potential Divisors:
A for loop iterates through numbers from 2 to number - 1. These are the potential divisors that are checked to see if they divide the input number evenly (i.e., number % i == 0).
Checking for Divisibility:
Inside the loop, the code checks if the current i evenly divides the input number (number % i == 0). If this condition is true, it means the input number is divisible by i, and therefore, it's not a prime number.
If divisibility is found, isPrime is set to False, indicating that the input number is not prime, and the loop is terminated using break.
Printing the Result:
After the loop finishes, the code prints the result based on the value of isPrime. If isPrime remains True after the loop, it means that the input number is not divisible by any of the numbers in the loop, making it prime. In this case, it prints that the number is prime.
If isPrime was set to `False during the loop, it indicates that the input number is divisible by at least one of the numbers in the loop, making it composite. In this case, it prints that the number is not prime.