# My Weblog

## SPOJ 8433. Revenge of the squares (variation)

SPOJ 8433. Revenge of the squares (variation) is well know problem. Just iterate from 1 to $\sqrt {\frac n 2 }$. Only constraint is source code should be less than 300 bytes and Haskell fits very well for this purpose. Accepted Haskell code.

s n=show$a where a=f n (truncate.sqrt.fromIntegral$div n 2 ) 1 0

f n p a cnt
|a>p=cnt
|otherwise=if(truncate.sqrt.fromIntegral$n-a*a)^2==(n-a*a) then f n p (a+1) (cnt+1) else f n p (a+1) cnt main = interact$ unlines.map ( s.read ) . lines


June 17, 2011 - Posted by | Programming | , ,