6 Replies - 1094 Views - Last Post: 03 October 2012 - 08:58 PM Rate Topic: -----

#1 Errk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-October 12

using recursion to print out odd numbers

Posted 03 October 2012 - 06:52 AM

def printA(num):
  if (num == 256):
    return;
  printNow(num);
  printA(num+1);
printA(1);


so basically i'm extremely new to coding and i need help, i don't expect the exact fix, but a push in the right direction would be awesome. i need to edit this code in some way to make the odd numbers between 1 and 255 print. currently it prints out all the numbers from 1 to 255, but i just need the odd. any help would be appreciated, thanks a ton in advanced :D

Is This A Good Question/Topic? 0
  • +

Replies To: using recursion to print out odd numbers

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,350
  • Joined: 19-March 11

Re: using recursion to print out odd numbers

Posted 03 October 2012 - 06:56 AM

I suggest an if statement - the standard test for odd or even depends on the number's remainder when divided by 2. (%2)
Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: using recursion to print out odd numbers

Posted 03 October 2012 - 07:07 AM

To clarify a tiny bit, 5 % 2 will equal 1. This is because 5 divided by two is two with a remainder of 1. Similarly 6 % 2 will equal 0. This is because 6 divided by two is three with a remainder of 0. So, we can conclude that any number mod 2 will equal either 0 or 1.

Mod stands for modulo operation

This post has been edited by atraub: 03 October 2012 - 07:08 AM

Was This Post Helpful? 1
  • +
  • -

#4 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

Re: using recursion to print out odd numbers

Posted 03 October 2012 - 07:33 PM

Ha! I had always wondered what % did but hadn't reached that part in my book. So what if I wanted to divide something as well as get the remainder? Would you use 5 /% 2 ?
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,350
  • Joined: 19-March 11

Re: using recursion to print out odd numbers

Posted 03 October 2012 - 07:41 PM

View Postmidknight51, on 03 October 2012 - 09:33 PM, said:

Ha! I had always wondered what % did but hadn't reached that part in my book. So what if I wanted to divide something as well as get the remainder? Would you use 5 /% 2 ?



That would make a lot of sense, wouldn't it? It would return a tuple (quotient, remainder)!
Sadly, though, that's not in the language yet. Talk to Guido.
Until your eminently sensible suggestion is taken up, though, you have to perform two operations, a div and a mod.
Was This Post Helpful? 1
  • +
  • -

#6 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

Re: using recursion to print out odd numbers

Posted 03 October 2012 - 07:52 PM

View Postjon.kiparsky, on 03 October 2012 - 09:41 PM, said:

That would make a lot of sense, wouldn't it? It would return a tuple (quotient, remainder)!
Sadly, though, that's not in the language yet. Talk to Guido.
Until your eminently sensible suggestion is taken up, though, you have to perform two operations, a div and a mod.


That's a darn shame. Oh well, one extra line of code for now isn't that big of a problem....yet.
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: using recursion to print out odd numbers

Posted 03 October 2012 - 08:58 PM

you could do something like
def div_mod(dividend, divisor): 
    return (dividend//divisor, dividend%divisor,)


Then you could do:
>>> div_mod(5,2)
(2,1)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1