加姬吧 关注:158贴子:3,930
  • 14回复贴,共1

【水星】My Solution To Project Euler In Mathematica

取消只看楼主收藏回复

Mathematica的For语句真是执行力低下,我只看重其做题的方便性,不能保证算法速度。试着写一下


IP属地:日本1楼2012-09-07 12:38回复
    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 all the multiples of 3 or 5 below 1000.
    分析:找出这个<100且为3或5倍数的集即可,为

    


    IP属地:日本2楼2012-09-07 12:42
    回复
      Problem 3
      The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
      分析:这种题目对mathematica毫无压力,调用下内置函数就行

      


      IP属地:日本4楼2012-09-07 12:52
      收起回复
        Problem 4
        A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.
        分析:暴力算之,先定义检测的函数PalindromeQ,然后对于{i*j}(i,j都是三位数)这个集筛选下就行了

        


        IP属地:日本5楼2012-09-07 12:56
        回复
          Problem 5
          2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
          分析:求最小公倍数,也无压力。

          


          IP属地:日本6楼2012-09-07 13:00
          回复
            Problem 6
            The sum of the squares of the first ten natural numbers is,
            1^2 + 2^2 + ... + 10^2 = 385
            The square of the sum of the first ten natural numbers is,
            (1 + 2 + ... + 10)^2 = 55^2 = 3025
            Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
            分析:就是等差/高阶等差数列之和,可以写出公式,不过直接加也简单

            


            IP属地:日本7楼2012-09-07 13:09
            回复
              Problem 8
              Find the greatest product of five consecutive digits in the 1000-digit number.
              73167176531330624919225119674426574742355349194934
              96983520312774506326239578318016984801869478851843
              85861560789112949495459501737958331952853208805511
              12540698747158523863050715693290963295227443043557
              66896648950445244523161731856403098711121722383113
              62229893423380308135336276614282806444486645238749
              30358907296290491560440772390713810515859307960866
              70172427121883998797908792274921901699720888093776
              65727333001053367881220235421809751254540594752243
              52584907711670556013604839586446706324415722155397
              53697817977846174064955149290862569321978468622482
              83972241375657056057490261407972968652414535100474
              82166370484403199890008895243450658541227588666881
              16427171479924442928230863465674813919123162824586
              17866458359124566529476545682848912883142607690042
              24219022671055626321111109370544217506941658960408
              07198403850962455444362981230987879927244284909188
              84580156166097919133875499200524063689912560717606
              05886116467109405077541002256983155200055935729725
              71636269561882670428252483600823257530420752963450
              分析:关键就在输入上,把这串数变成一个表就行了
              这只截一部分图

              


              IP属地:日本9楼2012-09-07 13:14
              回复
                Problem 9
                A Pythagorean triplet is a set of three natural numbers, a^2 + b^2 = c^2
                For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
                There exists exactly one Pythagorean triplet for which a + b + c = 1000.
                Find the product abc.
                分析:可以暴力算之:

                但我们也能手算得到结果:
                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
                即 m(m+n) = 500
                由于b>0,那么m>n,只有m=20,n=5满足条件,带入得到答案


                IP属地:日本10楼2012-09-07 13:23
                回复
                  Problem 10
                  The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million.
                  分析:先估计一个数,再利用内置函数计算下就行

                  


                  IP属地:日本11楼2012-09-07 13:32
                  回复
                    Problem 12
                    The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
                    1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
                    Let us list the factors of the first seven triangle numbers:
                    1:1
                    3:1,3
                    6:1,2,3,6
                    10:1,2,5,10
                    15:1,3,6,15
                    21:1,3,7,21
                    28:1,2,4,7,14,28
                    We can see that 28 is the first triangle number to have over five divisors.
                    What is the value of the first triangle number to have over five hundred divisors?
                    分析:注意三角数的公式是n(n+1)/2,先估计一个上界大概n为15000内有解,然后用Divisors这个函数就行了

                    


                    IP属地:日本14楼2012-09-07 13:47
                    回复
                      Problem 13:
                      Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
                      数字在:projecteuler.net/problem=13
                      分析:把这些数字转到一个表里面就行,只截图部分

                      


                      IP属地:日本16楼2012-09-07 13:58
                      回复
                        Problem 14
                        The following iterative sequence is defined for the set of positive integers:
                        n →n/2 (n is even)
                        n →3n + 1 (n is odd)
                        Using the rule above and starting with 13, we generate the following sequence:
                        13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
                        It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. Which starting number, under one million, produces the longest chain?
                        分析:这题Ms用mathematica做不知道要做到猴年马月去,我是用C语言搞的
                        PS:Mathematica写个10,000个都花了26s,如果是1,000,000估计至少要2600s,等不及那么久,不知道我这个算法在Mathematica是否最快:

                        还是用C算,答案是837799


                        IP属地:日本17楼2012-09-07 14:19
                        收起回复
                          Problem 15
                          Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner.

                          How many routes are there through a 20×20 grid?
                          分析:这题过于简单,只需C(40,20)算下就行了

                          


                          IP属地:日本18楼2012-09-07 14:27
                          收起回复
                            Problem 14的再讨论:
                            这个收回前言,的确有算法可以简化,见:

                            要来的快多了,看来我mathematica编程还不是很强。。Longwaytogo
                            


                            IP属地:日本19楼2012-09-07 17:45
                            回复
                              Problem 15
                              Starting in the top left corner of a 22 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.
                              How many such routes are there through a 2020 grid?
                              这个简单,直接数学的方法就知道了,就是



                              IP属地:日本26楼2013-09-30 12:35
                              回复