1.9 略
1.10 略
1.11
{- recursive style
f3' n
| n<3 = n
| otherwise = f3(n-1) + 2*f3(n-2) + 3*f3(n-3)
-}
getBegin n = n-(fromIntegral t)-1
where t = floor (n-3) ::Integer
f3 n
| n<3 = n
| otherwise = f3_iter b (b-1) (b-2) (n-2)
where b = getBegin n
f3_iter a _ _ 0 = a
f3_iter a b c k = f3_iter (a+2*b+3*c) a b (k-1)
1.12
pascal :: Integer -> Integer -> Integer
pascal n k
| k==1 = 1
| n==k = 1
| k<n = pascal (n-1) k + (pascal (n-1) (k-1))
| otherwise = error "Invalid!"
1.13 略
1.14 略
1.15 略
1.16
-- Iterative version of fast-exp
fast_exp :: (Num a)=> a -> Integer -> a
fast_exp b n = exp_iter b n 1
where exp_iter _ 0 a = a
exp_iter b n a = if (even n ) then exp_iter (b*b) (n `div` 2) a
else exp_iter b (n-1) (a*b)
1.17 略
1.18
fast_mult :: Integer -> Integer -> Integer
fast_mult a b = fast_iter a b 0
where fast_iter _ 0 s = s
fast_iter a b s = if (even b) then fast_iter (a*2) (b `div` 2) s
else fast_iter a (b-1) (s+a)
1.19
fast_fib :: Integer -> Integer
fast_fib n = fast_iter 1 0 0 1 n
where fast_iter _ b _ _ 0 = b
fast_iter a b p q count =
if (even count) then
fast_iter a b (p*p+q*q) (p*q+q*p+q*q) (count `div` 2)
else
fast_iter (b*q+a*q+a*p) (b*p+a*q) p q (count-1)
fib :: Integer -> Integer
fib 0 =1
fib 1 =1
fib n = fib (n-1) + fib (n-2)
1.20 略
1.21
smallestDivesor :: Integer -> Integer
smallestDivesor n = divTry 2 n
where divTry a n
| a*a > n = n
| n `mod` a ==0 = a
| otherwise = divTry (a+1) n
main :: IO ()
main = do mapM_ (putStrLn.show.smallestDivesor) [199,1999,19999]
1.22 略
1.23
smallestDivesor' :: Integer -> Integer
smallestDivesor' n = divTry 2 n
where divTry a n
| a*a > n = n
| n `mod` a ==0 = a
| otherwise = divTry (next a) n
next a = if a==2 then 3 else (a+2)
1.24~1.28 略 质数问题,以后总结
分享到:
相关推荐
Section 1.2. Origins Section 1.3. Features Section 1.4. Downloading and Installing Python Section 1.5. Running Python Section 1.6. Python Documentation Section 1.7. Comparing Python Section 1.8. Other...
Section 1.2. Navigation Applications Section 1.3. Content Model Section 1.4. Layout Section 1.5. Controls Section 1.6. Data Binding Section 1.7. Dependency Properties Section 1.8. ...
Section 1.2. Network Hardware Section 1.3. Network Software Section 1.4. Reference Models Section 1.5. Example Networks Section 1.6. Network Standardization Section 1.7. Metric Units ...
Section 1.2. Network Hardware Section 1.3. Network Software Section 1.4. Reference Models Section 1.5. Example Networks Section 1.6. Network Standardization Section 1.7. Metric Units ...
Section 1.2. Network Hardware Section 1.3. Network Software Section 1.4. Reference Models Section 1.5. Example Networks Section 1.6. Network Standardization Section 1.7. Metric Units Section 1.8...
Section 1.2. Network Hardware Section 1.3. Network Software Section 1.4. Reference Models Section 1.5. Example Networks Section 1.6. Network Standardization Section 1.7. Metric Units ...
Section 1.2. Network Hardware Section 1.3. Network Software Section 1.4. Reference Models Section 1.5. Example Networks Section 1.6. Network Standardization Section 1.7. Metric Units ...
Section 1.2. Network Hardware Section 1.3. Network Software Section 1.4. Reference Models Section 1.5. Example Networks Section 1.6. Network Standardization Section 1.7. Metric Units ...
Section 1.2. Network Hardware Section 1.3. Network Software Section 1.4. Reference Models Section 1.5. Example Networks Section 1.6. Network Standardization Section 1.7. Metric Units ...
Section 1.2. Web Application Architecture Blueprints Chapter 2. Installation and Configuration Section 2.1. Installation Section 2.2. Configuration and Hardening Section 2.3. Changing Web ...
### Chapter 1 Section 1.2 - Greedy Gift Givers (gift1) 该题目的难度与联赛第一题相当,要求计算每个人在分发礼物后的盈余。通过使用数组`incom`和`outcom`分别记录每个人的收入和支出,并利用哈希表优化,可以...
Section 1.2. Analyzing JavaScript Section 1.3. A Simple Testing Ground Chapter 2. Grammar Section 2.1. Whitespace Section 2.2. Names Section 2.3. Numbers Section 2.4. Strings Section 2.5. ...
Section 1.2. Embedded Linux Today Section 1.3. Open Source and the GPL Section 1.4. Standards and Relevant Bodies Section 1.5. Chapter Summary Chapter 2. Your First Embedded Experience Section 2.1. ...
Section 1.2. Security Section 1.3. But Wait! There's More! Chapter 2. Basic Network Concepts Section 2.1. Networks Section 2.2. The Layers of a Network Section 2.3. IP, TCP, and UDP ...
Section 1.2. Network Components at Different Layers Section 1.3. Overview of Internet Protocols Section 1.4. Summary Chapter 2. The Internet Protocol and Routing Section 2.1. Addressing ...
Section 1.2. A Quick Tour Section 1.3. Resources for Developers Using Stored Programs Section 1.4. Some Words of Advice for Developers Section 1.5. Conclusion Chapter 2. MySQL Stored ...
Section 1.2. What Type of Book Is This? Section 1.3. Terminology Section 1.4. Notation Section 1.5. Primer on Networking Section 1.6. Active vs. Passive Attacks Section 1.7. Layers and ...