Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Print even numbers in a list - Python
Next article icon

Python Program for Print Number series without using any loop

Last Updated : 23 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Problem – Givens Two number N and K, our task is to subtract a number K from N until number(N) is greater than zero, once the N becomes negative or zero then we start adding K until that number become the original number(N). Note : Not allow to use any loop. Examples :

Input : N = 15 K = 5   Output : 15 10 5 0 1 5 10 15  Input : N = 20 K = 6 Output : 20 14 8 2 -4 2 8 14 20 

Explanation – We can do it using recursion idea is that we call the function again and again until N is greater than zero (in every function call we subtract N by K). Once the number becomes negative or zero we start adding K in every function call until the number becomes the original number. Here we use a single function for both addition and subtraction but to switch between addition or subtraction function we used a Boolean flag. 

Python3




# Python program to Print Number
# series without using loop
 
def PrintNumber(N, Original, K, flag):
    #print the number
    print(N, end = " ")
     
    # change flag if number
    # become negative
     
    if (N <= 0):
        if(flag==0):
            flag = 1
        else:
            flag = 0
         
    # base condition for
    # second_case (Adding K)
     
    if (N == Original and (not(flag))):
        return
     
    # if flag is true
    # we subtract value until
    # number is greater than zero
     
    if (flag == True):
        PrintNumber(N - K, Original, K, flag)
        return
     
    # second case (Addition )
    if (not(flag)):
        PrintNumber(N + K, Original, K, flag);
        return
     
N = 20
K = 6
PrintNumber(N, N, K, True)
 
# This code is contributed by Mohit Gupta_OMG
 
 

Output :

20 14 8 2 -4 2 8 14 20 

The time complexity is O(N/K), where N is the input number and K is the step value.

The auxiliary space is O(N/K)

Approach Name: Recursive Mathematical Approach using Function Calls.

Steps:

  1. Define a function print_series(N, K) that takes two parameters, N and K.
  2. Check if N is less than or equal to zero, if yes, print N and return.
  3. Print N and call the print_series(N-K, K) function recursively.
  4. Print N again.

Python3




def print_series(N, K):
    if N <= 0:
        print(N, end=" ")
        return
    print(N, end=" ")
    print_series(N-K, K)
    print(N, end=" ")
 
N = 20
K = 6
print_series(N, K)
 
 
Output
20 14 8 2 -4 2 8 14 20 

 It has a time complexity of O(log(N/K)) and an auxiliary space of O(log(N/K)).

Please refer complete article on Print Number series without using any loop for more details!



Next Article
Print even numbers in a list - Python
author
kartik
Improve
Article Tags :
  • DSA
  • Python Programs

Similar Reads

  • 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 Print the Natural Numbers Summation Pattern
    Given a natural number n, the task is to write a Python program to first find the sum of first n natural numbers and then print each step as a pattern. Input: 5 Output: 1 = 11 + 2 = 31 + 2 + 3 = 61 + 2 + 3 + 4 = 101 + 2 + 3 + 4 + 5 = 15 Input: 10 Output: 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 =
    2 min read
  • Python program to print the binary value of the numbers from 1 to N
    Given a positive number N, the task here is to print the binary value of numbers from 1 to N. For this purpose various approaches can be used. The binary representation of a number is its equivalent value using 1 and 0 only. Example for k = 15, binary value is 1 1 1 1 Method 1: Using the Elementary
    2 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. [GFGTABS] Python start = -5 end = 3 #
    2 min read
  • Python Program to Find the Sum of Natural Numbers Using While Loop
    Calculating the Sum of N numbers in Python using while loops is very easy. In this article, we will understand how we can calculate the sum of N numbers in Python using while loop. Calculate Sum of Natural Numbers in Python Using While LoopBelow are some of the examples by which we can see how we ca
    2 min read
  • Python 3 | Program to print double sided stair-case pattern
    Below mentioned is the Python 3 program to print the double-sided staircase pattern. Examples: Input: 10Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Note: This code only works for even values of n. Example1 The given
    3 min read
  • Python program to print the octal value of the numbers from 1 to N
    Given a number N, the task is to write a Python program to print the octal value of the numbers from 1 to N. Examples: Approach: We will take the value of N as input.Then, we will run the for loop from 1 to N+1 and traverse each "i" through oct() function.Print each octal value.Note: The oct() funct
    4 min read
  • Python Program to Reverse a Number
    We are given a number and our task is to reverse its digits. For example, if the input is 12345 then the output should be 54321. In this article, we will explore various techniques for reversing a number in Python. Using String SlicingIn this example, the Python code reverses a given number by conve
    3 min read
  • Python program to print positive numbers in a list
    In this article, we will explore various methods to o print positive numbers in a list. The simplest way to do is by using for loop function. Using LoopThe most basic method for printing positive numbers is to use a for loop to iterate through the list and check each element. [GFGTABS] Python a = [-
    2 min read
  • Python program to print the hexadecimal value of the numbers from 1 to N
    Given a number N, the task is to write a Python program to print the hexadecimal value of the numbers from 1 to N. Examples: Input: 3 Output: 1 2 3 Input: 11 Output: 1 2 3 4 5 6 7 8 9 a bApproach: We will take the value of N as input.Then, we will run the for loop from 1 to N+1 and traverse each "i"
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences