# using recursion to print out odd numbers

Page 1 of 1

## 6 Replies - 1321 Views - Last Post: 03 October 2012 - 08:58 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294086&amp;s=56bd42cb5386136daaeff18161f68d14&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Errk

Reputation: 0
• 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

Is This A Good Question/Topic? 0

## Replies To: using recursion to print out odd numbers

### #2 jon.kiparsky

• Pancakes!

Reputation: 8441
• Posts: 14,560
• 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)

### #3 atraub

• Pythoneer

Reputation: 803
• Posts: 2,142
• 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

### #4 midknight51

Reputation: 20
• 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 ?

### #5 jon.kiparsky

• Pancakes!

Reputation: 8441
• Posts: 14,560
• Joined: 19-March 11

## Re: using recursion to print out odd numbers

Posted 03 October 2012 - 07:41 PM

midknight51, 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.

### #6 midknight51

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

## Re: using recursion to print out odd numbers

Posted 03 October 2012 - 07:52 PM

jon.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.

### #7 atraub

• Pythoneer

Reputation: 803
• Posts: 2,142
• 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)
```