Skip to content

Instantly share code, notes, and snippets.

@highercomve
Created August 20, 2013 12:58
Show Gist options
  • Save highercomve/6281013 to your computer and use it in GitHub Desktop.
Save highercomve/6281013 to your computer and use it in GitHub Desktop.
Sumatoria de X^N desde X -> 1
# Sumatoria a la N
def sumatoria_n(x, n)
(1..x).inject { |sum, i| sum += i**(n) }
end
# Derivaciones del método sumatoria_n
funciones = ["enteros", "cuadrados", "cubos", "cuarta"]
# Creación de métodos:
# sumatoria_enteros, sumatoria_cuadrados, etc.
funciones.each.with_index(1) do |method, n|
define_method "sumatoria_#{method}" do |x|
sumatoria_n(x, n)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment