• 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

Factorial of all numbers in a given range

 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question: Write a program that displays the factorial of all numbers in a given range. In each line display a number and its factorial.
What it means by factorial of all numbers in a given range ? Any output example would be much appreciated.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,
I think that means giving a list of factorials. Suppose you enter 4 into that program. The output would be something like:
1! = 1
2! = 2
3! = 6
4! = 24
 
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or possibly you are expected to ask the user for two integers (a low and a high),
and then compute and print the factorial of all integers in that range.

So Jeanne's answer would be for the range (1,4).
but your program should allow, for example, (5,13) and print 5!, 6!, ..., 13!
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for making this question clear
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have included 0! = 1.
Challenge, JJ: write your factorialRecursion method using the keyword return only once. It is quite easy.
Beware of using an int. You will get overflow errors after 12! (I think). I think long arithmetic would calculate accurate factorials up to 21! but I am not certain. For bigger numbers, use BigInteger.
Another challenge. Print the last 5,000 digits of 1,000,000!
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I modify code to below code
but get below error
What could be the issues ?
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think long arithmetic would calculate accurate factorials up to 21! but I am not certain. For bigger numbers, use BigInteger.

I change to long and using BigInteger, but still getting same exception.
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The factorial method recurses indefinitely. If the parameter is 0 or 1, it should return 0 or 1, without recursing.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:The factorial method recurses indefinitely. If the parameter is 0 or 1, it should return 0 or 1, without recursing.

I thought it will only execute inside if block ?
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd call such solution a hybrid which contains both, loops and recursive calls. And I'd try to avoid that, because it is no end confusion, and of course wrong, because you don't have a base case (stopping condition) in your recursive function.

If you are writing recursive program, don't use loops at all.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:write your factorialRecursion method using the keyword return only once. It is quite easy.


@OP what Campbell meant here, was starting with letter - beware, spoiler: T. Meaning you need to use T... operator.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, but I don't follow. I hope OP does. At least, he has now a factorial with only one return! But with one or more base cases incorporated, I would not mind two returns.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:I'm sorry, but I don't follow. I hope OP does. At least, he has now a factorial with only one return! But with one or more base cases incorporated, I would not mind two returns.


But his factorial program never ends. There is no base case in recursive call, so program is guaranteed to crash on all inputs excluding 0 and 1.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:I'm sorry, but I don't follow.


I think I know what you mean. Yes, my first post sounds not in the same way I was meant it to sound. So OP, better ignore it. Thanks, Piet.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need a T... anywhere. I shall give you a start:-I think 20 is correct. Not certain.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Joe wrote:. . .

There are two serious errors in that method.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try to recover myself:

You have 2 problems here, ok, 1, and 1 is just a suggestion or maybe let's say a challenge for you (@OP):

1. Your current recursive call of factorial has no base case, that means program is guaranteed to crash if inputs aren't 0 or 1, that's partially because you misunderstood what Campbell suggested. Try to use ternary operator, which will allow you to have only one return statement. More elegant solution. I think this is what Campbell meant. [edit] Now that we know, it seems Campbell meant different thing, apologies for confusion, going for coffee then not to confuse again.

2. Now is my challenge once you finish [1]. Try to accomplish this exercise without loops at all, meaning exchange 'for' loop also with recursive call, which in turn would make your program to have 2 recursive functions, one for the range of values to calculate their factorials, and other factorial function itself.

I hope it is better now. [edit] As we saw - not much.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that was one of the errors I saw: no base case.
I shall let OP work out what the other error is.

Yes, I was thinking that ?: would permit an elegant solution. It is good to become familiar with all the operators, and not to be scared of ?:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic