My Weblog

Blog about programming and math

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
Advertisements

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

2 Comments »

  1. can u please explain the algorithm here? I want to write the same code in C..
    thanks

    Comment by coder123 | September 25, 2011 | Reply

  2. -> if(truncate.sqrt.fromIntegral$n-a*a)^2==(n-a*a) then f n p (a+1) (cnt+1)

    what’s this line mean?

    Comment by metallica_fans | November 8, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: