A given number from 2 to N should have each number divisible outputted as [(2,4), (2,6),( 2,8),( 3,9),( 3,12), (3,15) .....] in Python 2/3
NOTE: Any number divisible by
n
(wheren
is a whole number -1, 2, 3, 4 ...
) is a multiple ofn
.
You can try:
n = int(input("Enter a number: "))
multiples = [(a, b) for a in range(2, n + 1) for b in range(2, n + 1) if b%a == 0 and a != b]
print (multiples)
where n
is the number "below" which the number of multiples is printed for a specific number, but using n + 1
prints the number of multiples "up to" n
(only if it is possible).
For example, when n = 10
, it will give this output, [(2, 4), (2, 6), (2, 8), (2, 10), (3, 6), (3, 9), (4, 8), (5, 10)]
.
This has two conditions: b%a == 0
, which makes sure that a
is not zero, as b / 0 == math error
and b%a
checks whether the second number is a factor of the first number, or not.
Without b%a == 0
, you would have:
[(2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 2), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 2), (4, 3), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 2), (5, 3), (5, 4), (5, 6), (5, 7), (5, 8), (5, 9), (6, 2), (6, 3), (6, 4), (6, 5), (6, 7), (6, 8), (6, 9), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 8), (7, 9), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 9), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8)]
Since you do not want a
equal to b
(3 == 3
), you can use a != b
, so don't have to worry about getting (3, 3)
or (4, 4)
etc.
To get the maximum number of multiples for a good range of numbers ( 2
to n
) , I would use a larger sample such as n = 50
, which will give this output:
[(2, 4), (2, 6), (2, 8), (2, 10), (2, 12), (2, 14), (2, 16), (2, 18), (2, 20), (2, 22), (2, 24), (2, 26), (2, 28), (2, 30), (2, 32), (2, 34), (2, 36), (2, 38), (2, 40), (2, 42), (2, 44), (2, 46), (2, 48), (2, 50), (3, 6), (3, 9), (3, 12), (3, 15), (3, 18), (3, 21), (3, 24), (3, 27), (3, 30), (3, 33), (3, 36), (3, 39), (3, 42), (3, 45), (3, 48), (4, 8), (4, 12), (4, 16), (4, 20), (4, 24), (4, 28), (4, 32), (4, 36), (4, 40), (4, 44), (4, 48), (5, 10), (5, 15), (5, 20), (5, 25), (5, 30), (5, 35), (5, 40), (5, 45), (5, 50), (6, 12), (6, 18), (6, 24), (6, 30), (6, 36), (6, 42), (6, 48), (7, 14), (7, 21), (7, 28), (7, 35), (7, 42), (7, 49), (8, 16), (8, 24), (8, 32), (8, 40), (8, 48), (9, 18), (9, 27), (9, 36), (9, 45), (10, 20), (10, 30), (10, 40), (10, 50), (11, 22), (11, 33), (11, 44), (12, 24), (12, 36), (12, 48), (13, 26), (13, 39), (14, 28), (14, 42), (15, 30), (15, 45), (16, 32), (16, 48), (17, 34), (18, 36), (19, 38), (20, 40), (21, 42), (22, 44), (23, 46), (24, 48), (25, 50)]
Hope this helps!
Python prime numbers for noobies
We know a number will be a prime number if and only if it is divisible by 1 and the number itself. If it is divisible by any number within the range from 2 to (number - 1) then it will not be a prime number.
So in the second loop of the given code we are checking whether the number is divisible by any number within the range 2 to (number - 1) or not.
Example 1:
number = 5
i values = 2, 3, 4
So here none of the i values divides 5. So, 5 is a prime number.
Example 2:
number = 6
i values = 2, 3, 4, 5
here 2 divides 6 so 6 is not a prime number.
Thank you.
Related Topics
How to Select One Object from the List of Objects
How to Split Single Row into Multiple Rows in Spark Dataframe Using Java
Mock User Console Input in Junit Test
Calling Mutiple Webservice At Same Time in Spring Java
Spring Boot Not Recognizing Application.Properties File
Server_Error: [Code] 1675030 [Message]: Error Performing Query
Alternative to If-Else Statements or Better Approach
Getting Java.Lang.Classnotfoundexception: Javax.Servlet.Servletcontext in Junit
Spring Jpa/Hibernate Transaction Force Insert Instead of Update
When I Run Mockito Test Occurs Wrongtypeofreturnvalue Exception
Bottomnavigationview - How to Avoid Recreation of Fragments and Reuse Them
Source Folder Is Not on the Java Build Class Path, Creating Java Package
How to Pass Variables Between Cucumber-Jvm Steps
How to Sort Arraylist of Objects by Timestamp and Get Last Five Elements
How to Upload a Document to Sharepoint With Java
Javax.Net.Ssl.Sslexception: Certificate Doesn't Match Any of the Subject Alternative Names
Deserialization With @Jsonsubtypes for No Value - Missing Property Error