Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

haskell - Define the function squarefact::Int -> Int that computes for any positive integer n the squared factorial (n!)^2 == (1 * ...* n)^2

I am trying to define a function that computes for any positive integer the square of its factorial

(I am a beginner in Haskell any tips or help is appreciated)

I have tried a couple different ways one i believe to work and one definition i don't understand why it doesn't work

Function i believe works:

 squarefact:: Int -> Int
 squarefact 0 = 1
 squarefact n = n * n * squarefact(n-1)

Function I don't understand why it doesn't work:

squarefact:: Int -> Int
squarefact 0 = 1
squarefact n = (n * squarefact(n-1) ) * (n * squarefact(n-1) )

An explanation and walk through of the dunctions defined would help me understand them better thanks.

question from:https://stackoverflow.com/questions/65836322/define-the-function-squarefactint-int-that-computes-for-any-positive-intege

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The equation

squarefact n = (n * squarefact(n-1) ) * (n * squarefact(n-1) )

could be rewritten in mathematical notation as

(n!)^2 = n * ((n-1)!)^2 * n * ((n-1)!)^2

but this identity is incorrect. The right hand side includes factors 1,2,....,n-1 four times instead of only two, as in the left hand side.

By comparison,

squarefact n = n * n * squarefact(n-1)

is correct, since on both sides all the factors occur exactly twice.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...