• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Recursion

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys i'm not sure if this belongs on beginner forums or here but ill try this first.

I need to make a recursive method(no loops) that chooses 'r' different things from a set of 'n' things.

My current method looks like this but i don't think i'm doing it right.



I passed it (50,5) and i'm getting 61 as output.
Thanks for any help you can provide!
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, looks like you are looking at solving combinations using C(n,r) = (n!) / (r! * (n-r)!)

From what I understand,
C(16,3) = (16*15*14) / (3*2*1) = 560

Similarly as per your example,
C(50,5) = (50*49*48*47*46)/(5*4*3*2*1) = 2118760

While we can solve such problems using both recursions and loops, why is it that you are actually choosing recursion specifically ? (Just curious)

 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and ... Welcome to Coderanch !!
 
Robert lillyblad
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh okay! That makes a lot more sense, the way the formula is written was confusing the hell outa me. And it's for my Data Structures and Algorithms class we're doing our section on recursion so we're forced to not use loops!
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert lillyblad wrote:...on recursion so we're forced to not use loops!


okay, then I think this hint would work for you..

(50*49*48*47*46)/(5*4*3*2*1) = 2118760 can be broken down as :

(50/5) * (49/4) * (48/3) * (47/2) * (46/1) = 2118760

i.e.
when n=50, r=5
when n=49, r=4
when n=48, r=3
...
when n=46, r=1  (break since r=1, we cannot reduce it further)


 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Robert,

is the method 'choosingR(int n, int r)' supposed to be equal to C(n, r)?

If so, then your recursion is incorrect, you should have that (so, the base case is: if (r == 0) return 1; else ...)
Another recursion that you could use is    (Pascals Triangle), but that is not very efficient.

If you work with faculties (like n!), then be careful: n! overflows an int pretty fast. Better to use longs, or BigIntegers.



 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert lillyblad wrote:. . . C(n,r) = n! / (r! * (n-r)!)

I remember that formula from school Maths. It is the combination formula.

// its forcing me to type this in here because of abbreviations.

Unfortunately we have had problems with people writing phone‑type abbreviations like r. Try r instead.

. . .

That method is a pure function and could therefore be marked static. The Sun style guide might be old, but it is still valid. Don't write if (b) return x; return y; but use the ?: operator.Some of those () are redundant. You have had corrections to the formula already. Have you got them to work?
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic