-
Notifications
You must be signed in to change notification settings - Fork 1
/
kaprekar.hs
82 lines (67 loc) · 2.33 KB
/
kaprekar.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
module Main (main) where
import Data.List qualified as L (sort, sortBy)
import Data.Ord (comparing, Down(..))
{-
function saa(n) {
var ns = Math.pow(n,2),
sn = String(ns),
l = sn.length,
fb = Number(sn.substring(0,l/2)),
lb = Number(sn.substring(l/2,l));
pure n == fb + lb;
}
for (var i = 0; i < 10000000; i++) {
if (saa(i)) console.log(i);
}
-}
-- import Data.List
-- splitIntoPieces n l =
{-
saa :: (Num n) => n -> Bool
saa n = n == sum piecesNums where
squared = n ** 2
stringSquared = show ns
piecesStrings = splitIntoPieces 2 stringSquared
piecesNums = map read piecesStrings :: [Int]
main :: IO ()
main = print $ filter saa [1..10000000]
-}
-- String with leading zeroes, reverse, int, add.
-- >>> revAdd 3285
kapIter ∷ Integer → Integer
kapIter n = (read . L.sortBy (comparing Data.Ord.Down) . show $ n) - (read . L.sort . show $ n)
-- https://stackoverflow.com/questions/7442892/repeatedly-applying-a-function-until-the-result-is-stable
convergeSingle ∷ Eq a ⇒ (a → a) → a → a
convergeSingle = until =<< ((==) =<<)
{-# WARNING convergeSingle "Partial function. Loops forever on some inputs." #-}
{-
1. Make a list of things dealt with
2. Make a list of things seen already
3. If seen anything before, add it to matches
4. If seen something twice, finish
-}
-- | Ugh, very complex. That's gonna be like... at least n^2
-- Maybe we can do it backwards... with a vector or something. Because finding from the beginning? Nah nah nah.
stable ∷ Eq a ⇒ [a] → [a]
stable xs = reverse $ go xs [] [] where
go ∷ Eq a ⇒ [a] → [a] → [a] → [a]
go [] _ _ = []
go (x:xs') seenOnce seenTwice = if any (== x) seenTwice then seenTwice else (case any (== x) seenOnce of
False -> go xs' (x : seenOnce) seenTwice
True -> go xs' seenOnce (x : seenTwice))
kap ∷ Integer → [Integer]
kap = stable . iterate kapIter
setAt ∷ k → v → [(k, v)] → [(k, v)]
setAt = undefined
{-}
grouped :: [(a, b)] -> [(a, [b])]
grouped xs = go xs [] where
go :: [(a, b)] -> [(a, [b])] -> [(a, [b])]
go [] sofar = sofar
go ((k, v):xs) sofar = case L.find ((== k) . fst) sofar of
Nothing -> go xs (k, [v]):sofar
Just (k, v) -> go xs (setAt k v:vs sofar)
-}
main ∷ IO ()
main = pure ()