Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #19661
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: How to do Combinations/Permutations in BlueJ |
| References | <9b534783-eb45-458d-8abb-a4c890553971@googlegroups.com> <8d95d877-c3c3-4d36-97f2-389424fdfc43@googlegroups.com> <k7gefs$bp8$1@dont-email.me> |
| Message-ID | <avQms.8830$J_3.6717@newsfe07.iad> (permalink) |
| Date | 2012-11-08 10:48 -0500 |
On 11/8/12 9:10 AM, Eric Sosman wrote:
> On 11/7/2012 11:53 PM, lebaz95@gmail.com wrote:
>> On Wednesday, November 7, 2012 8:28:19 PM UTC-6, leb...@gmail.com wrote:
>>> I would like to create a program that will do problems like
>>> (xa+yb)^z. But I would need to do things like (5!/3!(5-3)!) How would
>>> I get this done?
>>
>> rslt = 1;
>> for(i = e; i > 0; i --)
>> {
>> rslt *= i;
>> }
>>
>> I asked my brother and he helped me. e in this program is a user input
>> so you may replace it as you see fit.
>
> A warning: If `e' is greater than 12, this calculation will
> produce values too large for an `int':
>
> 479001600 = 12!
> 2147483647 = Integer.MAX_VALUE
> 6227020800 = 13!
>
> You can go somewhat higher by making `rslt' a `long':
>
> 2432902008176640000 = 20!
> 9223372036854775807 = Long.MAX_VALUE
> 51090942171709440000 = 21!
>
> ... but for anything over 20 even `long' will not suffice. You
> should make sure `e' is 20 or smaller (12 or smaller for `int'),
> or take a look at the BigInteger class.
>
Or, since you are dividing by factorials, you can factor them out to
start with.
5!/3!(5-3)! =
5*4*3*2*1/(3*2*1)(2*1) =
(5*4)/(2*1) * (3*2*1)/(3*2*1) =
5*4/2
The general formula being n!/r!(n-r)!
I believe it is possible to keep the results in the range of integers,
if the final result is.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to do Combinations/Permutations in BlueJ lebaz95@gmail.com - 2012-11-07 18:28 -0800
Re: How to do Combinations/Permutations in BlueJ lebaz95@gmail.com - 2012-11-07 20:53 -0800
Re: How to do Combinations/Permutations in BlueJ markspace <-@.> - 2012-11-07 23:03 -0800
Re: How to do Combinations/Permutations in BlueJ Gene Wirchenko <genew@ocis.net> - 2012-11-08 11:16 -0800
Re: How to do Combinations/Permutations in BlueJ Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-11-08 09:10 -0500
Re: How to do Combinations/Permutations in BlueJ Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-11-08 10:48 -0500
Re: How to do Combinations/Permutations in BlueJ Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-11-08 13:18 -0500
Re: How to do Combinations/Permutations in BlueJ Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-11-08 23:14 -0500
Re: How to do Combinations/Permutations in BlueJ Arne Vajhøj <arne@vajhoej.dk> - 2012-11-08 15:52 -0500
csiph-web