前言
Mathematica是一款十分强大的计算软件,类似Matlab,属于自动化编程,具体介绍可以看比特之理的这篇文章:我为什么喜欢Mathematica!。我打算用 Project Euler 来学习这个软件,题目我就不汉化了,与之相关的学习记录在这里,以后慢慢更新。同时,我对Mathematica的学习记录都会放在分类里。文末有一个小彩蛋;-)
Problem 1
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 these multiples of 3 or 5 below 1000.
Answer: 233168
Code 1:
sumar[a_,b_]:=a*(Quotient[b,a])(Quotient[b,a]+1)/2
sumar[3,999]+sumar[5,999]-sumar[15,999]
Code 2:
{Range[3,1000,3],Range[5,999,5]}//Flatten//DeleteDuplicates(*或Union*)//Total
Problem 2
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Answer: 4613732
Select[Table[Fibonacci[n], {n, 33}], EvenQ] // Total
我最喜欢的语言是?