[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Can anyone try to solve this problems?

M. Edward (Ed) Borasky

1/15/2007 3:00:00 PM

Cyrus Gabilla wrote:
> Hi to everybody.
>
> I am trying to solve these three problems in Ruby. I do not know if my solutions are really correct. I just wanted to ask help, if somebody can show their solutions. I would appreciated your help.
>
> 1. Write a one-line in irb using Range#inject to calculate 20!. Generalize this into a function.
> 2. Write a function to find the longest string in an array of strings.
> 3. Write an iterator function n_times(n) that calls the given block n times.
>
You say you have solutions but you don't know if they are correct or
not. Perhaps you need to learn how to test correctness. As an example,
the calculation of 20 factorial -- does Ruby have a factorial built in?
If so, you could compare its results with the results from your code. If
not, find another language that does, or borrow someone's engineering
calculator to get the (rather large) number.

The other two are easy to test -- factorial is moderately difficult
because it's such a common exercise for students that languages tend not
to build it in, and because the resulting number gets very large very
quickly; unless a language has facilities for computing and printing
integers beyond machine precision.

So, courtesy of the Axiom system, here's 20! you can cut and paste into
your test driver. I ran off 50! too because ... well, because I can. :)

(2) -> factorial(20)
(2) ->
(2) 2432902008176640000
Type:
PositiveInteger
(3) -> factorial(50)
(3) ->
(3) 30414093201713378043612608166064768844377641568960512000000000000
Type:
PositiveInteger
(4) ->

--
M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)
http://borasky-research.blo...

If God had meant for carrots to be eaten cooked, He would have given rabbits fire.