module Factory.Math.PerfectPower(
maybeSquareNumber,
isPerfectPower
) where
import qualified Data.IntSet
import qualified Data.Set
import qualified Factory.Math.Power as Math.Power
maybeSquareNumber :: Integral i => i -> Maybe i
maybeSquareNumber :: i -> Maybe i
maybeSquareNumber i
i
| ((i, [i]) -> Bool) -> [(i, [i])] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\(i
modulus, [i]
valid) -> i -> i -> i
forall a. Integral a => a -> a -> a
rem i
i i
modulus i -> [i] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [i]
valid) [
(i
16, [i
0,i
1,i
4,i
9]),
(i
9, [i
0,i
1,i
4,i
7]),
(i
17, [i
1,i
2,i
4,i
8,i
9,i
13,i
15,i
16,i
0]),
(i
13, [i
1,i
3,i
4,i
9,i
10,i
12,i
0]),
(i
7, [i
1,i
2,i
4,i
0]),
(i
5, [i
1,i
4,i
0])
] Bool -> Bool -> Bool
&& i -> i
forall n. Num n => n -> n
Math.Power.square i
iSqrt i -> i -> Bool
forall a. Eq a => a -> a -> Bool
== i
i = i -> Maybe i
forall a. a -> Maybe a
Just i
iSqrt
| Bool
otherwise = Maybe i
forall a. Maybe a
Nothing
where
sqrt' :: Double
sqrt' :: Double
sqrt' = Double -> Double
forall a. Floating a => a -> a
sqrt (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ i -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral i
i
iSqrt :: i
iSqrt = Double -> i
forall a b. (RealFrac a, Integral b) => a -> b
round Double
sqrt'
isPerfectPower :: Integral i => i -> Bool
isPerfectPower :: i -> Bool
isPerfectPower i
i
| i
i i -> i -> Bool
forall a. Ord a => a -> a -> Bool
< i -> i
forall n. Num n => n -> n
Math.Power.square i
2 = Bool
False
| Bool
otherwise = i
i i -> Set i -> Bool
forall a. Ord a => a -> Set a -> Bool
`Data.Set.member` (i -> Set i -> Set i) -> Set i -> [i] -> Set i
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (
\i
n Set i
set -> if i
n i -> Set i -> Bool
forall a. Ord a => a -> Set a -> Bool
`Data.Set.member` Set i
set
then Set i
set
else (i -> Set i -> Set i) -> Set i -> [i] -> Set i
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr i -> Set i -> Set i
forall a. Ord a => a -> Set a -> Set a
Data.Set.insert Set i
set ([i] -> Set i) -> (i -> [i]) -> i -> Set i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (i -> Bool) -> [i] -> [i]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (i -> i -> Bool
forall a. Ord a => a -> a -> Bool
<= i
i) ([i] -> [i]) -> (i -> [i]) -> i -> [i]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (i -> i) -> i -> [i]
forall a. (a -> a) -> a -> [a]
iterate (i -> i -> i
forall a. Num a => a -> a -> a
* i
n) (i -> Set i) -> i -> Set i
forall a b. (a -> b) -> a -> b
$ i -> i
forall n. Num n => n -> n
Math.Power.square i
n
) Set i
forall a. Set a
Data.Set.empty [i
2 .. Double -> i
forall a b. (RealFrac a, Integral b) => a -> b
round (Double -> i) -> Double -> i
forall a b. (a -> b) -> a -> b
$ Double -> Double
forall a. Floating a => a -> a
sqrt (i -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral i
i :: Double)]
{-# NOINLINE isPerfectPower #-}
{-# RULES "isPerfectPower/Int" isPerfectPower = isPerfectPowerInt #-}
isPerfectPowerInt :: Int -> Bool
isPerfectPowerInt :: Int -> Bool
isPerfectPowerInt Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Int
forall n. Num n => n -> n
Math.Power.square Int
2 = Bool
False
| Bool
otherwise = Int
i Int -> IntSet -> Bool
`Data.IntSet.member` (Int -> IntSet -> IntSet) -> IntSet -> [Int] -> IntSet
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (
\Int
n IntSet
set -> if Int
n Int -> IntSet -> Bool
`Data.IntSet.member` IntSet
set
then IntSet
set
else (Int -> IntSet -> IntSet) -> IntSet -> [Int] -> IntSet
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Int -> IntSet -> IntSet
Data.IntSet.insert IntSet
set ([Int] -> IntSet) -> (Int -> [Int]) -> Int -> IntSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Bool) -> [Int] -> [Int]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
i) ([Int] -> [Int]) -> (Int -> [Int]) -> Int -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int) -> Int -> [Int]
forall a. (a -> a) -> a -> [a]
iterate (Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n) (Int -> IntSet) -> Int -> IntSet
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall n. Num n => n -> n
Math.Power.square Int
n
) IntSet
Data.IntSet.empty [Int
2 .. Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round (Double -> Int) -> Double -> Int
forall a b. (a -> b) -> a -> b
$ Double -> Double
forall a. Floating a => a -> a
sqrt (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i :: Double)]