Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

4lab_fp

.hs
Скачиваний:
7
Добавлен:
19.01.2023
Размер:
3 Кб
Скачать
☆
--1 задание
 
--import Data.Array
--main1 :: IO()
--main1 = do
	--contents <- readFile "input.txt"
	--writeFile "output.txt"(process contents)
	
--process :: String -> String
--process = 
	--unlines.map foo.lines
	
	--foo1::IO()
	--foo1 = readFile "input.txt" >>= writeFile "output.txt" . unlines . map (\s -> (replicate(div(80 - length s)1)' ')++s)
	
	
--foo s =(replicate(div(80 - length s)1)' ')++s

--------------------
import Data.List
import Char
import List
foooo :: IO ()
foooo = do
    contents <- readFile "input.txt"
    writeFile "output.txt" (process contents)

process :: String -> String	
process = unlines.map foo.lines
foo s =(replicate(div(80 - length s)1)' ')++s


--2 задание
-- Способ 1
 
--sumd :: Integer -> Integer
--sumd n = hsd n 2 (n+1)
            -- where hsd n k c | k*k>n          = c
                                  -- | n `mod` k == 0 = if k*k==n then c+k else hsd n (k+1) (c+k+n `div` k)
                                  -- | otherwise      = hsd n (k+1) c
 
-- Способ 2
 
--nsumd :: Integer -> Integer
--nsumd n = 0 + (sum $ filter (\x -> (n `mod` x == 0)&&(n/=x)) [1..n `div` 2 + 1])

--sumd :: Integer -> Integer
--sumd n = hsd n 2 (n+1)
            -- where hsd n k c 	| k*k>n = c
						--		| (n `mod` k == 0) = if k*k==n then c+k else hsd n (k+1) (c+k+n `div` k)
						--		| otherwise= hsd n (k+1) c

--foo_main = foo [1,2,3] 

--foo[]= []
--foo(x:xs) = nsumd x:foo xs

--summ_list [] = 0
--summ_list (x:xs) = nsumd x + summ_list xs



-- Primes from problem 3
primes = 2:filter isPrime [3,5..]
isPrime = null . tail . primeFactors
primeFactors n = factor n primes
    where
        factor n (p:ps)
            | p * p > n    = [n]
            | mod n p == 0 = p:factor (div n p) (p:ps)
            | otherwise    = factor n ps


-- Количество множителей "n"
-- Вычислить простые множители
-- Произведение каждой экспоненты + 1
-- 120 = 2 * 2 * 2 * 3 * 5 = 2 ^ 3 * 3 ^ 1 * 5 ^ 1
-- количество = (3 + 1) * (1 + 1) * (1 + 1) = 16
divisor = product . map ((1+) . length) . group . primeFactors
-- sum = (1+2^1+2^2+2^3) * (1+3^1) * (1+5^1)
sigma = product . map ((+1).foldl1 (\a x -> x+a*x)).group.primeFactors
aliquot n = sigma n - n

amicablePairs = [(a,b) | a <- [1..], let b = aliquot a, a < b, aliquot b == a]

solution = sum . map addTup.takeWhile under10k $ amicablePairs
  where
    addTup (a,b) = a+b
    under10k (a,b) = a < 10000

main = do
    putStr "Solution: "
    putStrLn $ show solution
	
	
	
-- 3 задание
--Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, 
--contain the same digits in some order.
has_same_digits a b = (show a) \\ (show b) == []

check n = all (has_same_digits n) (map (n*) [2..6])

problem_52 = head $ filter check [1..]

--------------------------
--Considering natural numbers of the form, ab, finding the maximum digital sum.
digitalSum 0 = 0
digitalSum n = 
    let (d,m) = quotRem n 10 in m + digitalSum d 
 
problem_56 = 
    maximum [digitalSum (a^b) | a <- [99], b <- [90..99]]