[projecteuler.net] 5;6;9

來源:互聯網
上載者:User
5
歐幾裡德求最小公約數法
f#實現:
///Euclidean algorithm///http://en.wikipedia.org/wiki/Euclidean_algorithm///求 A B的最大公約數///使用輾轉相減法///原始的歐幾裡演算法是:///設C為最大公約數///A=aC    B=bC///gcd( A,B) => gcd(A-B,B)  [a>b]  及gcd( (a-b)C , B) 不斷的用大的減去小的。直到a-b =1那麼就得到C了
let rec gcd a b =        match a with        | x when x=0I -> b        | _ -> gcd (b%a) a
有了最小公約數最小公倍數就簡單了
let  lcm a b =        a*b/ (gcd a b)第五題題目是求1到20的最小公倍數
那麼只要這樣既可:
let rec lcms xs =        match xs with        | a:: b::t-> lcms (lcm a b::t)        | a::[]         -> a
lcms [1..20]    
================================================================================================

6

記兩個公式先: (1+2+3...+n)^2 = ((1+n)*n/2)^2 (1^2 + 2^2 + 3^2 +...+ n^2) = 1/6 * n(n+1)(2n+1)  ===============================================================================================

 

9

///A Pythagorean triplet  Checklet isPt a b c =        a*a+b*b=c*clet sumEq1000 a b c= a+b+c=1000 let help predicate =        let mutable ret=[]        for a in 1 ..998 do                for b in (a+1)..999 do                        for c in (b+1)..1000 do                                if isPt a b c && predicate a b c then ret<-(a,b,c)::ret        ret let findPt =help sumEq1000

///prie解法: http://projecteuler.net/thread=9///Without programming://////a= 2mn; b= m^2 -n^2; c= m^2 + n^2;///a + b + c = 1000;//////2mn + (m^2 -n^2) + (m^2 + n^2) = 1000;///2mn + 2m^2 = 1000;///2m(m+n) = 1000;///m(m+n) = 500;//////m>n;//////m= 20; n= 5;//////a= 200; b= 375; c= 425;/// Pythagorean triplet k will be:/// k { m^2 - n^2, 2 m n , m^2 + n^2 } /// x = a + b + c let ptPire x =        let mutable ret=[]        for m in 1..x/2 do                for n in 1..m-1 do                        let a =m*m-n*n in                        let b =2*m*n in                        let c =m*m+n*n in                        if a+b+c=x && a*a+b*b=c*c then ret <- (a,b,c)::ret        ret

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.