Python Program to Print all Integers that Aren’t Divisible by Either 2 or 3
We can input a set of integer, and check which integers in this range, beginning with 1 are not divisible by 2 or 3, by checking the remainder of the integer with 2 and 3.
Example:
Input: 10
Output: Numbers not divisible by 2 and 3
1
5
7
Method 1: We check if the number is not divisible by 2 and 3 using the and clause, then outputs the number.
Python3
max_num = 20
n = 1
print ( "Numbers not divisible by 2 and 3" )
while n < = max_num:
if n % 2 ! = 0 and n % 3 ! = 0 :
print (n)
n = n + 1
|
Output
Numbers not divisible by 2 and 3
1
5
7
11
13
17
19
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 2: We traverse the odd numbers starting with 1 since even numbers are divisible by 2. So, we increment the for a loop by 2, traversing only odd numbers and check, which one of them is not divisible by 3. This approach is better than the previous one since it only iterates through half the number of elements in the specified range.
The following Python code illustrates this :
Python3
max_num = 40
print ( "Numbers not divisible by 2 or 3 : " )
for i in range ( 1 , max_num, 2 ):
if i % 3 ! = 0 :
print (i)
|
Output
Numbers not divisible by 2 or 3 :
1
5
7
11
13
17
19
23
25
29
31
35
37
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 3: Using List comprehension.
Python3
max_num = 40
result = [x for x in range ( 1 , max_num, 2 ) if x % 3 ! = 0 ]
print ( "Numbers not divisible by 2 or 3 : " , result)
|
Output
Numbers not divisible by 2 or 3 : [1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: looping and filtering
- Ask the user to input an integer value and store it in a variable.
- Create an empty list to store the integers that aren’t divisible by either 2 or 3.
- Use a for loop to iterate through a range of integers from 1 to the input integer value (inclusive).
- For each integer in the range, use an if statement to check whether it is not divisible by 2 or 3 using the modulo operator (%).
- If the integer is not divisible by 2 or 3, append it to the list of not divisible integers.
- Print the list of integers that aren’t divisible by either 2 or 3.
Python3
not_divisible = []
for i in range ( 1 , 11 ):
if i % 2 ! = 0 and i % 3 ! = 0 :
not_divisible.append(i)
print ( "Numbers not divisible by 2 or 3:" )
for num in not_divisible:
print (num)
|
Output
Numbers not divisible by 2 or 3:
1
5
7
The time complexity of this program is O(n),
The auxiliary space used by this program is also O(n).
Method 5: iteration with nested if statements
The steps for the “iteration with nested if statements” method to print all integers that aren’t divisible by either 2 or 3 are as follows:
- Define a function print_numbers_not_divisible_by_2_or_3 that takes an integer n as input.
- Use a for loop to iterate through all integers from 1 to n (inclusive) using the range() function.
- For each integer i in the loop, check whether it’s not divisible by 2 using the % operator (i.e., i % 2 != 0).
- If i is not divisible by 2, check whether it’s also not divisible by 3 using another nested if statement (i.e., if i % 3 != 0:).
- If i is not divisible by 2 and not divisible by 3, print it using the print() function.
Python3
def print_numbers_not_divisible_by_2_or_3(n):
for i in range ( 1 , n + 1 ):
if i % 2 ! = 0 :
if i % 3 ! = 0 :
print (i)
print_numbers_not_divisible_by_2_or_3( 10 )
|
The time complexity is O(n), as the loop iterates over all integers from 1 to n and checks each integer for divisibility by 2 and 3.
The auxiliary space complexity of this method is O(1), as it uses only a constant amount of extra memory for the loop variable i.
Overall, this method is efficient and uses minimal auxiliary space, making it suitable for handling large inputs.
Method 6: Iteration with a single if statement and modulo operator
- Initialize an empty list to store the integers that are not divisible by either 2 or 3.
- Iterate over the range of integers from 1 to the input number, both inclusive.
- Check if the current integer is not divisible by either 2 or 3 by checking if the remainder of the division by 2 and 3 is not equal to 0.
- If the current integer is not divisible by either 2 or 3, append it to the list of integers that are not divisible by either 2 or 3.
- After iterating over all the integers, print the list of integers that are not divisible by either 2 or 3.
Python3
n = 10
non_divisible = []
for i in range ( 1 , n + 1 ):
if i % 2 ! = 0 and i % 3 ! = 0 :
non_divisible.append(i)
print ( "Numbers not divisible by 2 and 3" )
for num in non_divisible:
print (num)
|
Output
Numbers not divisible by 2 and 3
1
5
7
Time complexity: O(n) where n is the input number.
Auxiliary space: O(m) where m is the number of integers that are not divisible by either 2 or 3.
Approach: Iteration with a single if statement, modulo operator and filter function:
Steps:
- Read the input integer from the user and store it in a variable.
- Initialize a loop variable i to 1.
- Run a loop from i to the input integer (inclusive).
- Check if the loop variable i is not divisible by 2 and 3 using the modulo operator.
- If the loop variable i is not divisible by 2 and 3, then print it.
- Increment the loop variable i by 1.
- Repeat steps 4 to 6 until the loop reaches the input integer.
Python3
num = 10
def check_divisibility(x): return (x % 2 ! = 0 ) and (x % 3 ! = 0 )
range_obj = range ( 1 , num + 1 )
filtered_nums = filter (check_divisibility, range_obj)
for num in filtered_nums:
print (num)
|
Time Complexity: O(N), where n is the input integer.
Auxiliary Space: O(1)
Last Updated :
22 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...