diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-09-05 14:48:42 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-09-05 14:48:42 +0200 |
| commit | f942937de1496e38f2d58d6383d03f5e254abfb6 (patch) | |
| tree | 9fd786b6988dffd17b8ebf4d227be0a17a412ea9 | |
| parent | 87f0a52d00b38198bbfa36b5febb257078080d6e (diff) | |
| download | project_euler-f942937de1496e38f2d58d6383d03f5e254abfb6.tar.gz project_euler-f942937de1496e38f2d58d6383d03f5e254abfb6.tar.bz2 project_euler-f942937de1496e38f2d58d6383d03f5e254abfb6.zip | |
problem 58 haskell
| -rw-r--r-- | haskell/058-spiral_primes.hs (renamed from haskell/wip/058-spiral_primes.hs) | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/haskell/wip/058-spiral_primes.hs b/haskell/058-spiral_primes.hs index 3c620ec..d2c10d1 100644 --- a/haskell/wip/058-spiral_primes.hs +++ b/haskell/058-spiral_primes.hs @@ -24,16 +24,16 @@ main = do - print $ firstDropBellow 0.1 3 0 + print $ firstDropBellow 0.1 0 0 -firstDropBellow :: Double -> Int -> Int -> (Int, Int, Double) +firstDropBellow :: Double -> Int -> Int -> Int firstDropBellow percent counter n - | primesRatio < percent = (n, 2 * n + 1, primesRatio) + | counter /= 0 && primesRatio < percent = 2 * n + 1 | otherwise = firstDropBellow percent (counter + countPrimes) (n + 1) where primesRatio = (fromIntegral countPrimes + fromIntegral counter) / fromIntegral (4 * n + 1) - countPrimes = length $ filter id (map isPrime corners) - corners = [dur, ddl, dul] + countPrimes = length $ filter id (map isPrime diags) + diags = [dur, ddl, dul] dur = 4 * n ^ 2 - 2 * n + 1 ddl = 4 * n ^ 2 + 2 * n + 1 dul = 4 * n ^ 2 + 1 @@ -49,8 +49,3 @@ isPrime x | d * d > x = True | x `mod` d == 0 || x `mod` (d + 2) == 0 = False | otherwise = divCheck (d + 6) - --- diagUpRight = [4 * x ^ 2 - 2 * x + 1 | x <- [1..]] --- diagdownLeft = [4 * x ^ 2 + 2 * x + 1 | x <- [1..]] --- diagUpLeft = [4 * x ^ 2 + 1 | x <- [1..]] --- diags = [diagUpRight, diagdownLeft, diagUpLeft] |
