Open In App

Python Program to Print all Integers that Aren’t Divisible by Either 2 or 3

Improve
Improve
Like Article
Like
Save
Share
Report

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




# input the maximum number to
# which you want to send
max_num = 20
 
# starting numbers from 0
n = 1
 
# run until it reaches maximum number
print("Numbers not divisible by 2 and 3")
while n <= max_num:
     
    # check if number is divisible by 2 and 3
    if n % 2 != 0 and n % 3 != 0:
        print(n)
     
    # incrementing the counter
    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




# input the maximum number to
# which you want to send
max_num = 40
print("Numbers not divisible by 2 or 3 : ")
 
# run until it reaches maximum number
# we increment the loop by +2 each time,
# since odd numbers are not divisible by 2
for i in range(1, max_num, 2):
     
    # check if number is not divisible by  3
    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




# input the maximum number to which you want to find numbers not divisible by 2 or 3
max_num = 40
 
# using list comprehension to find numbers not divisible by 2 or 3
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

  1. Ask the user to input an integer value and store it in a variable.
  2. Create an empty list to store the integers that aren’t divisible by either 2 or 3.
  3. Use a for loop to iterate through a range of integers from 1 to the input integer value (inclusive).
  4. 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 (%).
  5. If the integer is not divisible by 2 or 3, append it to the list of not divisible integers.
  6. 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:

  1. Define a function print_numbers_not_divisible_by_2_or_3 that takes an integer n as input.
  2. Use a for loop to iterate through all integers from 1 to n (inclusive) using the range() function.
  3. For each integer i in the loop, check whether it’s not divisible by 2 using the % operator (i.e., i % 2 != 0).
  4. 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:).
  5. 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)


Output

1
5
7

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

  1. Initialize an empty list to store the integers that are not divisible by either 2 or 3.
  2. Iterate over the range of integers from 1 to the input number, both inclusive.
  3. 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.
  4. 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.
  5. After iterating over all the integers, print the list of integers that are not divisible by either 2 or 3.

Python3




n = 10
 
# initialize empty list to store non-divisible integers
non_divisible = []
 
# iterate over range of integers from 1 to n
for i in range(1, n+1):
    # check if the integer is not divisible by either 2 or 3
    if i % 2 != 0 and i % 3 != 0:
        # if the integer is not divisible by either 2 or 3, append it to the list
        non_divisible.append(i)
 
# print the list of integers that are not divisible by either 2 or 3
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:

  1. Read the input integer from the user and store it in a variable.
  2. Initialize a loop variable i to 1.
  3. Run a loop from i to the input integer (inclusive).
  4. Check if the loop variable i is not divisible by 2 and 3 using the modulo operator.
  5. If the loop variable i is not divisible by 2 and 3, then print it.
  6. Increment the loop variable i by 1.
  7. Repeat steps 4 to 6 until the loop reaches the input integer.

Python3




# Reading input integer from the user
num = 10
 
# Defining lambda function to check if
# the number is not divisible by 2 or 3
def check_divisibility(x): return (x % 2 != 0) and (x % 3 != 0)
 
# Creating range object from 1 to the
# input integer (exclusive)
range_obj = range(1, num+1)
 
# Using filter function to filter out the
# numbers that are divisible by 2 or 3
filtered_nums = filter(check_divisibility, range_obj)
 
# Iterating through the filtered numbers
# and printing them
for num in filtered_nums:
    print(num)


Output

1
5
7

Time Complexity: O(N), where n is the input integer.
Auxiliary Space: O(1)



Last Updated : 22 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads