Python Program to Print all Integers that Aren't Divisible by Either 2 or 3
Last Updated :
22 Apr, 2023
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
OutputNumbers 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)
OutputNumbers 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)
OutputNumbers 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)
OutputNumbers 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
# 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)
OutputNumbers 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
# 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)
Time Complexity: O(N), where n is the input integer.
Auxiliary Space: O(1)
Similar Reads
Python Program to print all the numbers divisible by 3 and 5 for a given number
Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5.Examples : Input : 50Output : 0 15 30 45 Input : 100Output : 0 15 30 45 60 75 90 Approach: For example, let's take N = 20 as a limit, then the program should print all numbers less than 20 which are
2 min read
Program to print all the numbers divisible by 3 and 5 for a given number
Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5.Examples : Input : 50Output : 0 15 30 45 Input : 100Output : 0 15 30 45 60 75 90 Approach: For example, let's take N = 20 as a limit, then the program should print all numbers less than 20 which are
9 min read
Program to print all the numbers divisible by 5 or 7 for a given number
Given the integer N, the task is to print all the numbers less than N, which are divisible by 5 or 7. Examples : Input : 20 Output : 5 7 10 14 15 20 Input: 50 Output: 5 7 10 14 15 20 21 25 28 30 35 40 42 45 49 50 Approach: For example, letâs take N = 20 as a limit, then the program should print all
4 min read
Python Program to Print all Prime numbers in an Interval
The task of printing all prime numbers in an interval in Python involves taking two input values representing a range [x, y] and finding all prime numbers within that range. A prime number is a natural number greater than 1 that is divisible only by 1 and itself. For example, in the interval [2, 7],
4 min read
Python Program to Find Numbers Divisible by 7 and Multiple of 5 in a Given Range
Given a range of numbers, the task is to write a Python program to find numbers divisible by 7 and multiple of 5. Example: Input:Enter minimum 100 Enter maximum 200 Output: 105 is divisible by 7 and 5. 140 is divisible by 7 and 5. 175 is divisible by 7 and 5. Input:Input:Enter minimum 29 Enter maxim
5 min read
Python Program to Find Numbers Divisible by Another Number
We are given a list of numbers and a number. We have to find all the numbers in the list that are divisible by the given single number. Examples:Input: list=[8, 14, 21, 36, 43], num=3Output: 21, 36, 57Input: list=[2, 17, 25, 31, 48, 55], num=5Output: 25, 55In this article, we will discuss the differ
3 min read
Python program to print even numbers in a list
Getting even numbers from a list in Python allows you to filter out all numbers that are divisible by 2. For example, given the list a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], you might want to extract the even numbers [2, 4, 6, 8, 10]. There are various efficient methods to extract even numbers from a li
3 min read
Python Program to Find XOR of array elements which are divisible by given number
Given an array arr[] containing integers of size N and a number k, the task is to find the XOR of array elements that are divisible by a given number k in Python.Examples: Input: arr[] = {3, 9, 16, 12, 13, 15} , k = 3Output: 25Explanation: Only 3,9,12,15 are divisible by 3, XOR = 3^9^12^15 = 9Input:
4 min read
Python program to print all positive numbers in a range
In this article, we will explore various methods to print all positive numbers in a range. The simplest way to do this is by using a loop. Use a simple for loop to iterate through the given range and check if each number is greater than zero before printing it.Pythonstart = -5 end = 3 # Loop through
2 min read
Python Program to Print All Pronic Numbers Between 1 and 100
Pronic numbers are also called oblong numbers or rectangular numbers. They are produced by multiplying two successive integers. Mathematically, it can be written as n * (n + 1), where n is a positive integer. For instance, 6 is a pronic number because it equals 2 * (2 + 1). Similarly, when you multi
2 min read