用Python解答ProjectEuler問題(1)
有個很有意思的網站 ProjectEuler.net ,提出了200多道數學問題,要求讀者用計算機求解,不限制所用的計算機語言。
(2008年11月)試著用Python做了幾道,挺有意思的。
- Add all the natural numbers below one thousand that are multiples of 3 or 5.
- Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.
- Find the largest prime factor of a composite number.
- Find the largest palindrome made from the product of two 3-digit numbers.
- What is the smallest number divisible by each of the numbers 1 to 20?
E001
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
求1000以下,所有是3或5倍數的數之和。
def problem1(): a, b, c = 3, 5, 999 f = lambda x,lmt=c: x*(lmt/x)*(lmt/x+1)/2 return f(a)+f(b)-f(a*b) if __name__==`__main__`: print str(problem1())
關鍵是 1000以內能被3整除的數之和是 3(1+2+3+…+333)
相關文章
- Python list,dict問題解答Python
- C# 面試問題與解答1C#面試
- Python程式設計常見問題與解答Python程式設計
- JVM問題及解答JVM
- Python工程師必看的面試問題與解答(中)Python工程師面試
- APatch常見問題解答
- 【答疑】物件儲存OSS常見問題解答(工具類1)物件
- RapidWeaver 8常見問題解答API
- Ubuntu 常見問題和解答Ubuntu
- 軟體新人問題解答(一)
- Docker面試問題與解答Docker面試
- 【答疑】物件儲存OSS常見問題解答(諮詢類1)物件
- Python資料分析初體驗,需要解答的三大問題!Python
- ViewModel和LiveData問題思考與解答ViewLiveData
- [譯] HTTP/2 常見問題解答HTTP
- NSIS 之 NsDialogs 常見問題解答
- Rhinoceros 6 for Mac的常見問題解答ROSMac
- Screaming Frog SEO Spider常見問題解答IDE
- 小遊戲引擎常見問題解答遊戲引擎
- 關於CleanMyMac常見問題與解答Mac
- HTTPS總結+相關面試問題解答HTTP面試
- 犀牛Rhinoceros 6 for Mac的常見問題解答ROSMac
- SOLIDWORKS認證考試常見問題解答Solid
- 業務規則的常見問題解答
- Apple M1晶片常見問題解答:您想要了解的這裡都有APP晶片
- 提出問題,解答問題!這才是理解程式碼設計的正確方法
- GNU GPL 許可證常見問題解答(三)
- 30 個 Openstack 經典面試問題和解答面試
- 37個 JavaScript 基本面試問題和解答JavaScript面試
- hybris backoffice建立product遇到的synchronization問題和解答
- 全志RV1108常見問題操作解答
- MongoDB常見問題解答:時間與時區MongoDB
- vue-cli多頁面history模式-問題解答Vue模式
- 域名解析常見問題盤點及解答
- 關於“等保保護”最常見問題解答!
- [提問交流]OneThink學習日記(更有很多問題求解答!)
- 面試被問TopK問題,可以這樣優雅的解答面試TopK
- 微課|玩轉Python輕鬆過二級:第3章課後習題解答1Python
- 條形碼設計軟體BarTender實用教程——模板物件常見問題解答物件