Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...