Steady squares: Difference between revisions

→‎{{header|Haskell}}: Added another variant
(→‎{{header|Haskell}}: Added another variant)
Line 467:
 
 
or retaining the string pairs from the tests:
Or, obtaining the squares by addition, rather than multiplication:
<lang haskell>import Control.Monad (join)
import Data.Bifunctor (bimap)
import Data.List (isSuffixOf)
 
---------------------- STEADY NUMBERS --------------------
 
steadyPair :: Int -> [(String, String)]
steadyPair n =
[ (s, s2)
| let (s, s2) = join bimap show (n, n * n),
s `isSuffixOf` s2
]
 
--------------------------- TEST -------------------------
main :: IO ()
main =
( \xs ->
let (w, w2) = join bimap length (last xs)
in mapM_
( putStrLn . uncurry ((<>) . (<> " -> "))
. bimap
(justifyRight w ' ')
(justifyRight w2 ' ')
)
xs
)
$ [0 .. 10000] >>= steadyPair
 
------------------------- GENERIC ------------------------
 
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</lang>
{{Out}}
<pre> 0 -> 0
1 -> 1
5 -> 25
6 -> 36
25 -> 625
76 -> 5776
376 -> 141376
625 -> 390625
9376 -> 87909376</pre>
 
 
Or,or obtaining the squares by addition, rather than multiplication:
<lang haskell>import Control.Monad (join)
import Data.Bifunctor (bimap)
9,655

edits