Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Removed input
module Main ( nth_term, main ) where all_equal :: (Eq e) => [e] -> e -> Bool all_equal [] _ = False all_equal [el] x = el == x all_equal (el:ys) x = el == x && all_equal ys x all_same :: (Eq e) => [e] -> Bool all_same [] = False all_same li@(x:xs) = all_equal li x get_steps :: (Num a) => [a] -> [a] get_steps xs = [xs !! (n + 1) - xs !! n | n <- [0..length xs - 2]] nth_term :: (Num a, Ord a, Show a, Fractional a, Floating a) => [a] -> String nth_term xs = if all_same xs then show $ head xs else if all_same steps then let step = head steps add = head xs - step d_step = if step == 1 then "" else show step d_add = if add == 0 then "" else if add > 0 then '+':(show add) else show add in d_step ++ "n" ++ d_add else let n_steps = get_steps steps in if all_same n_steps then let n_step = head n_steps coefficient = n_step * 0.5 normal = [(fromIntegral n ** 2) * coefficient | n <- [1..length xs - 1]] difference = [j - k | (j, k) <- zip xs normal] nth_diff = nth_term difference d_coefficient = if coefficient == 1 then "" else show coefficient d_nth_diff = if head nth_diff == '0' then "" else (if head nth_diff == '-' then "" else "+") ++ nth_diff in d_coefficient ++ "n²" ++ d_nth_diff else "Invalid" where steps = get_steps xs main = do putStrLn $ nth_term [3, 9, 19]
run
|
edit
|
history
|
help
0
Znajdz trójkąty prostokątne
Token
project euler 2, haskell
Calculate Integral of function
tried some polynoms 2: now with some `*` and foldr(-p)
equiv_strings
equiv
Monoid and friends
Reader
ReadablePractice