Why we use count variable & and how its work? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
#program of prime number from 2 to 100
for i in range (2,100):
count = 0
for j in range(1, i+1):
if i % j = 0:
count = count + 1
if count == 2:
print(i)

First of all, your code seems to not work due to a syntax error(might be a typo).
if conditions in python, requires double equals (==).
if i % j == 0:
In the question you posted, the count variable acts as a flag or counter variable.
A prime number is a number which can only be perfectly divided, by 1 or the number itself. (eg: 11 can be divided by 1 and 11 only, therefore a prime number)
So the counter variable i.e. count holds the number of times that a number(2-200) gets perfectly divided(i.e. (%) modulo operation results in value 0). If the value of count is 2 then we can confirm that it is a prime number since a prime number can only be divided with 1 and the number itself (hence count=2). And if the count == 2 after all the possible divisions happening in the j loop (2nd for loop), we can conclude that it is a prime number.
for i in range (2,200):
count = 0
for j in range(1, i+1):
if i % j == 0:
count += 1
if count == 2:
print(i)

count variable is being used to ensure if the particular number i is being divisible by any other number other than 1. Because if it does, then it is not a prime number. The moment it becomes 2, it means that number is divisible by 1 and some other number less than i proving it be a non-prime number.

Run this to get primes from 100, you can change the 100 to whatever you want
for num in range(100):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")

Related

please explain me how this program works? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
def print_prime_1_n(n):
for i in range(1, n+1):
prime = True
for j in range(2, i):
if i % j == 0:
prime = False
break
if prime:
print(i)
print_prime_1_n(10)
I spent 2 days to figure out this code but i coudn't find a way to figout it out. please explain to me?
I hope this helps:
def print_prime_1_n(n):
# For all numbers from 1 to n+1
for i in range(1, n+1):
# Assume i is prime until proven otherwise
prime = True
# For all numbers from 2 to i (second number used to check if i is prime)
for j in range(2, i):
# If i is divisible by j, then i is not prime because it is divisible by a number other than 1 and itself
if i % j == 0:
# Set prime to False
prime = False
# Short circuit the loop because we know i is not prime
break
# If prime is still True, then i is prime and we can print it
if prime:
print(i)
print_prime_1_n(10)
This function is a naive (or brute force) algorithm that prints all prime numbers between 1 and n
From the wiki: A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers
How does this function works?
We're looping through all natural numbers between 1 and n that we want to check (is it a prime?)
for i in range(1, n+1):
By default at the beginning we think that this number is prime
prime = True
And if this variable stays true after checking, we're printing current number
if prime:
print(i)
And the final step is to check for prime
for j in range(2, i):
if i % j == 0:
prime = False
break
i % j means remainder of division of i by j
i - our current number we're checking
j - the number less than i by which we are trying to divide
If the i % j equals to 0, then i is divisible without a remainder by j, so our number is composite, not prime, as it has a divisor
Therefore we set prime = False and exiting our inner loop with break (we don't have to check further)
Note that we loop from 2 to i because upper bound i is not included, so we're looping between 2 and i - 1
def is_prime(i):
# if I is divisible by any number smaller than it, it is not prime
prime = True
for j in range(2, i):
if i % j == 0: # divisibility check
prime = False
break
return prime
def print_prime_1_n(n):
for i in range(1, n+1):
if is_prime(i):
print(i)
print_prime_1_n(10)

How to print just the final sum of odd and even integers? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Built a program that allows the user to input a number, then calculates the sum of all the odd integers and all the even integers between 1 and the input. How do I only print the final sum of the odd and even integers rather than the sum between each addition? Thank you!
#Allow user to enter a number
n = input("Enter a number: ")
n = int(n)
n >= 1
total = 0
#sum of even numbers
for n in range(1, n+1):
if n % 2 == 0:
total += n
print("Sum of even numbers: ", total)
#sum of odd numbers
total2 = 0
for n in range(1, n+1):
if n % 2 == 1:
total2 += n
print("Sum of odd numbers: ", total2)
As mentioned before, you need to modify your indentation. Your code should look like that:
n = input("Enter a number: ")
n = int(n)
total = 0
#sum of even numbers
for n in range(1, n+1):
if n % 2 == 0:
total += n
print("Sum of even numbers: ", total)
#sum of odd numbers
total2 = 0
for n in range(1, n+1):
if n % 2 == 1:
total2 += n
print("Sum of odd numbers: ", total2)
Instead of making 2 different loops, you can do it in one single loop too. Like the one below:
num = input("Enter a number: ")
num = int(num)
even = [] # Appends all the even integers present in the user-accepted input
odd = [] # Appends all the odd integers present in the user-accepted input
for n in range(1, num+1):
if n % 2 == 0:
even.append(n)
else:
odd.append(n)
print("Sum of Even integers : ", sum(even))
print("Sum of Odd integers : ", sum(odd))

Count simple numbers in diapasone python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Task is : input two numbers that is a diapasone . Then count a quantity of simple numbers into this diapasone.
Please correct my python code below
import math
count = (int, input().split())
for i in count:
n = int(input())
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
print(count)
If you want to count the prime numbers in the closed interval [j, k] (which is given with input()), then you could use
import math
interval = tuple(map(int, input().split()))
count = 0
for n in range(max(2, interval[0]), interval[1]+1):
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
else:
count += 1
print(count)
Explanation:
use map to apply int on each string
use count as a counter variable
use range as an iterator over integers in given interval
start prime number tests with 2, even if lower bound is smaller
count up only if no divisor is found (else clause of for loop)

Find all the divisors of a number using Python [duplicate]

This question already has answers here:
What is the best way to get all the divisors of a number?
(18 answers)
Closed 2 years ago.
I am trying to write a Python script that will find prime numbers. In the way that I am going about this, the script is going to take a number from a range, determine the factors, determine if the only two factors are 1 and itself, and return true if it's a prime number. I need help with my code to find the factors.
num = int(input("enter: "))
i = 1
while i < num:
if num % i == 0:
print(i)
i += 1
else:
pass
The i is set to 1 because you can't divide by 0. However, the return starts to work, however, it only returns a couple of numbers, not the full list that I am hoping for. For example, you input 20, it returns only 1 and 2.
You're only incrementing i in the "is even" case. This'd fix it:
num = int(input("enter: "))
i = 1
while i < num:
if num % i == 0:
print(i)
i += 1
However, it's better to use range() for iterating over a range of numbers:
num = int(input("enter: "))
for i in range(1, num):
if num % i == 0:
print(i)

How to exclude a number from a range in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm new to coding, and I wrote a program using a for loop and a range to check if a number from that range is odd or even. However I want to exclude the number 0
for num in range (0, 11):
if num %2 == 0:
print (num,' is an even number!')
else:
print (num,' is an odd number!')
I expected the output of all the numbers but not the 0.
could someone help me?
As answers have already mentioned, range(1, 11) will produce 1 2 3 4 5 6 7 8 9 10 as requested. However you can exclude specific numbers by doing:
all_numbers = range(0, 11)
exclude_set = {0} # a set containing the value 0
numbers = (num for num in all_numbers if num not in exclude_set)
This could be useful if you have a set of numbers that you know are wrong, but otherwise have a contiguous range.
"""Represent retail stores in a chain"""
store_numbers = range(1, 51)
# stores 14, 17, and 32 have been closed, so
closed_stores = {14, 17, 32}
valid_stores = (store for store in store_numbers if store not in closed_stores)
If you want to exclude 0 then change your range to (1,11). The way range works is the lower limit is inclusive where as upper limit is exclusive.
Docs to read more about range: https://docs.python.org/3/library/functions.html#func-range
On an unrelated note, if your lower limit is 0, you need not include the lower limit and write it as range(11) which is same as range(0,11).
Range is inclusive of the first parameter, but does not include the second parameter. The first number in range should be the number you want to start with.
Change your code to
for num in range(1, 11):
if num % 2 == 0:
print(num, ' is an even number!')
else:
print(num, ' is an odd number!')

Categories