Jump to content

First perfect square in base n with n unique digits: Difference between revisions

m
→‎{{header|Haskell}}: Applicative formulation of `digits` turns out, FWIW, to be a little faster.
m (→‎{{header|Haskell}}: Applicative formulation of `digits` turns out, FWIW, to be a little faster.)
Line 799:
import qualified Data.Set as Set
import Text.Printf (printf)
 
digits :: Integral a => a -> a -> [a]
digits b = unfoldr (\d -> guard (d /= 0) >> pure (d `mod` b, d `div` b))
(((>>) . guard . (0 /=)) <*> (pure . ((,) <$> (`mod` b) <*> (`div` b))))
 
sequenceForBaseN :: Integral a => a -> [a]
sequenceForBaseN
b = unfoldr (\(v, n) -> Just (v, (v + n, n + 2))) (i ^ 2, i * 2 + 1)
where
i = succ $ (round $ sqrt (realToFrac $ (b ^ pred b)))
 
searchSequence :: Integral a => a -> Maybe a
searchSequence
Line 814 ⟶ 815:
where
digitsSet = Set.fromList [0 .. pred b]
 
display :: Integer -> Integer -> String
display b n = fmapmap (intToDigit . fromIntegral) .$ reverse .$ digits b n
 
main :: IO ()
main = mapM_
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.