Calculation of Ruby Factorial

Asked

Viewed 582 times

0

Create a Ruby script that reads 10 whole numbers and store them in an array. Then the script should calculate the factorial of each of these 10 numbers, and store the results in another array, and print the values.

I can’t get the factorial and storage calculation right in the other array(array2). I ask for your help!

My code:

array1 = []

for i in 0..10
  puts "Digite um numero:"
  array1.push(gets.to_i)
end

def fatorial (n)
    if(n > 1)
       return n * fatorial
    else
       return 1
    end

n = gets.to_i ()
fat = fatorial (n)
array2 = n ==1
array2 << array1

puts "Os números armazenados são: #{n} + #{array2}"
end
  • No one will do for you. Do it yourself, in case any problem arises, then yes come to stackoverflow.

  • Hello Brena, Share what you have so far, please. I believe you are beginner. And algorithms need to be handled in the effort of each of us. Try! One hour you get there!

  • Good morning, I’m new to the forum, this is my first post. I didn’t know the rules, thank you for guiding me. I ask for your help to solve this exercise, because I want to learn.

1 answer

1

Just for the record, using recursion to calculate factorial is not the most efficient way to do it, and in real applications you shouldn’t use just because it’s possible (prefer the iterative version). That said, let’s go to the answer


First of all, its function fatorial has 2 problems:

  • missing the last end at the end to close it
  • when the number is greater than 1, you must call n * fatorial(n - 1) (then call only fatorial, without passing parameters, causes an error, because the function expects a number as a parameter). This follows the mathematical definition, for example, 5! = 5 * 4! - generalizing, n! = n * (n - 1)! (the factorial of a number is this number times the factorial of this number minus 1). Therefore the expression must be n * fatorial(n - 1). If you do not pass any parameter (write only fatorial, as you did), the function has no way of knowing which number to use.

Then she’d be like this:

def fatorial (n)
    if (n > 1)
       return n * fatorial(n - 1)
    else
       return 1
    end
end

The last end should not stay after the puts, and yes right after the if-else, for that is where the function fatorial ends.


As for the arrays, I don’t quite understand what you want to do. You are creating the array1, filling with multiple numbers and then you don’t use these numbers for anything (just to copy them to the array2).

You are also putting the factorial result into the variable fat, but then you don’t use this variable for anything.

If what you want is to create a array2 containing the factorial result of the numbers that are in array1 (and this is my guess, because it’s not clear what you want), so you can do something like this:

array2 = []
# para cada número em array1, calcular o fatorial e armazenar em array2
array1.each { |n|  array2.push(fatorial(n)) }

Or else:

array2 = array1.map { |n| fatorial(n) }

With this, every number of the array2 corresponds to the factorial of the number which is in the corresponding position in array1. For example, if the array1 have the numbers [2, 3, 7], the array2 shall have their respective factors ([2, 6, 5040]).

If that’s not what you want, edit your question explaining better what you are trying to do, input and output examples (the one you want and the one the program returns), error messages that appear, etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.