• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

sum of odd and even  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Would like to ask.
How do I combine these two functions:


AND


 
Marshal
Posts: 63461
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same as you would for any recursion: use 1 and 2 for your base case. In Java® I would writeYou will of course get an “infinite” recursion if i ≤ 0
 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java it is more clear and understandable for me...
I just started python in uni, getting used to it...
Question is - in python
 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wrong for odd
 
Sheriff
Posts: 21649
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Same as you would for any recursion: use 1 and 2 for your base case. In Java® I would writeYou will of course get an “infinite” recursion if i ≤ 0


The quotes around infinite are correct, because it would definitely end because of overflow. The method will be recursively called until the argument becomes Integer.MIN_VALUE or Integer.MIN_VALUE + 1. When you subtract 2 from that it will overflow to Integer.MAX_VALUE - 1 or Integer.MAX_VALUE respectively.

Of course you'll run into a StackOverflowError long before it even gets there.
 
Greenhorn
Posts: 26
Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vadim,

In addition to the other ways of hitting some limit, Python also has a builtin recursion default limit of 1000.  It can be changed by using sys.setrecursionlimit() if desired. However, for most Python programs, hitting that limit (or one of the others) means that the algorithm can be improved to avoid hitting any of these limits.  🙂
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!