-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the solutions of problems from 1 to 10 written in Haskell
- Loading branch information
Showing
13 changed files
with
84 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
import qualified Data.Char as Char | |||
|
|||
digStrProd :: String -> Int | |||
digStrProd digStr = | |||
product $ map (\c -> Char.ord c - Char.ord '0') digStr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
liutos@liutos-laptop.7207:1332138436 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
pro1 :: Int -> Int | |||
pro1 sup = sum [x | x <- [1..(sup - 1)], x `mod` 3 == 0 || x `mod` 5 == 0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
isPrime :: Int -> Bool | |||
isPrime number = | |||
number /= 1 && rec 2 | |||
where rec n | |||
| n * n > number = True | |||
| 0 == number `mod` n = False | |||
| otherwise = rec (n + 1) | |||
|
|||
pro10 = | |||
sum [ n | n <- [1..200000], isPrime n] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,6 @@ | |||
pro2 limit = | |||
let rec st nd sum = | |||
if nd > limit | |||
then sum | |||
else rec nd (st + nd) (if even nd then sum + nd else sum) | |||
in rec 1 2 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,15 @@ | |||
pro3Helper :: Integer -> Integer | |||
pro3Helper n = | |||
let rec testNum | |||
| testNum >= n || 0 == n `mod` testNum = testNum | |||
| otherwise = rec (testNum + 1) | |||
in rec 2 | |||
|
|||
pro3 :: Integer -> Integer | |||
pro3 n = | |||
let rec num acc | |||
| 1 == num = acc | |||
| num <= 2 = num | |||
| otherwise = rec (num `div` fac) fac | |||
where fac = pro3Helper num | |||
in rec n 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
isPalindromic :: Int -> Bool | |||
isPalindromic number = | |||
let str = show number | |||
in str == reverse str | |||
|
|||
pro4 = | |||
maximum [x * y | x <- [100..999], y <- [100..999], isPalindromic (x * y)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
helper :: Int -> [Int] -> Int | |||
helper n lst = | |||
foldl (\n d -> if 0 == n `mod` d then n `div` d else n) n lst | |||
|
|||
pro5 :: Int -> Int | |||
pro5 n = | |||
let rec m lst = | |||
if m >= n | |||
then product lst | |||
else rec (m + 1) (helper m lst : lst) | |||
in rec 2 [1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
pro6 :: Int -> Int | |||
pro6 n = | |||
let tmp = (n * (n + 1)) `div` 2 | |||
in tmp * tmp - sum [ x * x | x <- [1..n]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,15 @@ | |||
isPrime :: Int -> Bool | |||
isPrime number = | |||
number /= 1 && rec 2 | |||
where rec n | |||
| n * n > number = True | |||
| 0 == number `mod` n = False | |||
| otherwise = rec (n + 1) | |||
|
|||
pro7 :: Int -> Int | |||
pro7 lim = | |||
rec 2 0 | |||
where rec test cnt | |||
| lim == cnt = test - 1 | |||
| isPrime test = rec (test + 1) (cnt + 1) | |||
| otherwise = rec (test + 1) cnt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
import qualified Data.Char as Char | |||
|
|||
digStrProd :: String -> Int | |||
digStrProd digStr = | |||
product $ map (\c -> Char.ord c - Char.ord '0') digStr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
pro9 = | |||
[(a, b, c, a * b * c) | a <- [1..1000], b <- [a..1000], c <- [b..1000], 1000 == a + b + c, a * a + b * b == c * c] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters