Dividing integer by all numbers in a range in Python

user2975192

I'm trying to write a script that divides an integer by all numbers in a range and have a value of true only if all numbers are to 0 decimal places.

Here is the code I have written so far:

n = int(input("Please enter a number (more than 0) :"))
count = 0


if n < 1 : print("Please enter a positive number")
else:
    if n%2 == 0 : print("Number is not prime")
    else:

        for i in range(3, int(n**0.5)+1):
                if i % n == 0 :
                    count = count + 1
                    count = count + 0

if count > 1 : print("Number is not prime")
else : print("Number is prime")

It prints Number is prime for any odd number.

Any ideas why this doesn't work?

Thanks.

shad0w_wa1k3r

You are wrong on two counts -

        for i in range(3, int(n**0.5)+1):
            if n%i == 0 : # You want to check if n is divisible by i
                count = count + 1
                # No need of count = count + 0 here since it does nothing

if count > 0 : print("Number is not prime") # Prime if count > 0 & not count > 1
else : print("Number is prime")

Also, you can improve the code by running the for loop in steps of 2 rather than 1 i.e. check if n is divisible by all odd numbers from 3 to sqrt(n) instead of all numbers from 3 to sqrt(n).

for i in range(3, int(n**0.5)+1, 2): # The 3rd argument indicates steps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dividing integer to get all of it's numbers and sum them

From Dev

Python dividing numbers

From Dev

dividing all numbers in a file using shell scripting

From Dev

In python, how to generate a random integer in a range, excluding some numbers in a list?

From Dev

Dividing an Integer by Its Reverse in Python 3

From Dev

Dividing IP range into chunk of 1024 using python

From Dev

Sum all numbers in a given range of a given list Python

From Dev

Python: Print all numbers in range divisible by x and y

From Dev

return sum all numbers in a range

From Dev

Python: Prime numbers and the in range()

From Dev

trouble with a range of numbers python

From Dev

Dividing an integer into lists

From Dev

Integer dividing columns - SQLite

From Dev

Dividing in Python

From Dev

Dividing a number into groups of numbers

From Dev

Python Range() for positive and negative numbers

From Dev

Python regex search range of numbers

From Dev

Assigning range of numbers to a letter in python

From Java

Adding sum of all odd numbers with range JAVA

From Dev

How to get all numbers between a range

From Dev

Loop through all numbers in a range except a few

From Dev

Javascript - Show all telephone numbers in a range

From Dev

Adding all the numbers in a Range, including the number

From Dev

Dividing all the numbers in a data frame by a chosen row in the same data frame and corresponding column position in R

From Dev

Dividing slider values into range segments

From Dev

Faster algorithm to count how many numbers are divisible by a specific integer in a range

From Dev

Generate integer random numbers from range (0:10^12)

From Dev

Faster algorithm to count how many numbers are divisible by a specific integer in a range

From Dev

Dividing numbers from columns csv

Related Related

  1. 1

    Dividing integer to get all of it's numbers and sum them

  2. 2

    Python dividing numbers

  3. 3

    dividing all numbers in a file using shell scripting

  4. 4

    In python, how to generate a random integer in a range, excluding some numbers in a list?

  5. 5

    Dividing an Integer by Its Reverse in Python 3

  6. 6

    Dividing IP range into chunk of 1024 using python

  7. 7

    Sum all numbers in a given range of a given list Python

  8. 8

    Python: Print all numbers in range divisible by x and y

  9. 9

    return sum all numbers in a range

  10. 10

    Python: Prime numbers and the in range()

  11. 11

    trouble with a range of numbers python

  12. 12

    Dividing an integer into lists

  13. 13

    Integer dividing columns - SQLite

  14. 14

    Dividing in Python

  15. 15

    Dividing a number into groups of numbers

  16. 16

    Python Range() for positive and negative numbers

  17. 17

    Python regex search range of numbers

  18. 18

    Assigning range of numbers to a letter in python

  19. 19

    Adding sum of all odd numbers with range JAVA

  20. 20

    How to get all numbers between a range

  21. 21

    Loop through all numbers in a range except a few

  22. 22

    Javascript - Show all telephone numbers in a range

  23. 23

    Adding all the numbers in a Range, including the number

  24. 24

    Dividing all the numbers in a data frame by a chosen row in the same data frame and corresponding column position in R

  25. 25

    Dividing slider values into range segments

  26. 26

    Faster algorithm to count how many numbers are divisible by a specific integer in a range

  27. 27

    Generate integer random numbers from range (0:10^12)

  28. 28

    Faster algorithm to count how many numbers are divisible by a specific integer in a range

  29. 29

    Dividing numbers from columns csv

HotTag

Archive