---
title: "Project Euler解题汇总 051 ~ 060"
author: "Eastsun"
date: "2008-09-12T22:19:19Z"
tags: []
categories: ["编程语言"]
slug: "project-euler-051-060"
draft: false
lastmod: "2008-09-12T22:19:19Z"
id: 241358
url: "https://eastsun.iteye.com/blog/241358"
---

<span style="color:red">注</span>：本文代码中会使用[按字典顺序生成所有的排列](http://eastsun.iteye.com/admin/blogs/207614)与[筛法求素数](http://eastsun.iteye.com/admin/blogs/240339)中介绍的函数。

[问题51](http://projecteuler.net/index.php?section=problems&id=51)：<span style="color:orange">Find the smallest prime which, by changing the same part of the number, can form eight different primes.</span>
<span style="color:red">题目简介</span>：对于数字56**3(其中*表示占位符)，将其中的两个*换成0~9中的数字，产生的10个数字中为素数的有：56003, 56113, 56333, 56443, 56663, 56773, 56993。<span style="color:olive">56003</span>是这一系列素数中最小的那个。
　　现在要求满足下列条件最小的那个素数：该数是由将某个数中的几位（不一定相邻）替换为相同数字而产生的8个素数中的一个。（<span style="color:red">注意</span>：如果被替换的位置中有首位，则不能用0来替换）
<span style="color:red">答　　案</span>：121313

```java
import java.util.Arrays.fill
import eastsun.math.Util._

object Euler051 extends Application {
    
    var pa = getPrimes(1000000).filter{ _ > 100000 }
    var bf = new Array[int](64)
    var res = 0
    var i = 0
    while(res == 0 && i < pa.length){
        fill(bf,1)
        var j = i + 1
        while(res == 0 && j < pa.length){
            var pi = pa(i)
            var pj = pa(j)
            var id = 0
            var tg = true
            var pie = -1
            var pje = -1
            do{
                id = id<<1
                if(pi%10 == pj%10) id = id|1
                else if(pie != -1 && (pj%10 != pje || pi%10 != pie)) tg =false
                else{ 
                    pje = pj%10
                    pie = pi%10
                }
                pi = pi/10
                pj = pj/10
            }while(pi != 0 && tg )
            if(tg){
                bf(id) += 1
                if(bf(id) >= 8 ) res = pa(i)
            }
            j += 1
        }
        i += 1
    } 
    println(res)   
}
```



[问题52](http://projecteuler.net/index.php?section=problems&id=52)：<span style="color:orange">Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits in some order.</span>
<span style="color:red">题目简介</span>：求最小的x，使得x的2倍，3倍，4倍，5倍，6倍都是由相同的数字组成
<span style="color:red">答　　案</span>：142857
PS：对1/7的循环节印象比较深刻，直接就写出来了，果然是对的:-)


[问题53](http://projecteuler.net/index.php?section=problems&id=53)：<span style="color:orange">How many values of C(n,r), for 1 ≤ n ≤ 100, exceed one-million?</span>
<span style="color:red">题目简介</span>：求组合数C(n,r) (1 ≤ n ≤ 100)中超过1000000的有多少个。
<span style="color:red">答　　案</span>：4075

```java
object Euler053 extends Application {
    var buf =new Array[bigint](101)
    var res =0
    for(row <- 0 to 100){
        var pre =buf(0)
        for(col <- 1 to row-1){
            var tmp =pre
            pre =buf(col)
            buf(col) += tmp
            if(buf(col)>1000000) res += 1
        }
        buf(row) =1
    }
    println(res)
}
```



[问题54](http://projecteuler.net/index.php?section=problems&id=54)：<span style="color:orange">How many hands did player one win in the game of poker?</span>
<span style="color:red">题目简介</span>：一个扑克牌游戏，判断提供的数据中玩家1赢的盘数有多少次
<span style="color:red">答　　案</span>：376

```java
import scala.io.Source._
object Euler054 extends Application {
    val src = fromFile("poker.txt").getLines
    var cnt = 0
    src.foreach{ hand =>
        val lst = hand.split("\\s").toList
        val aPlayer = new CardHand(lst.take(5))
        val bPlayer = new CardHand(lst.drop(5))
        if(aPlayer > bPlayer) cnt += 1
    }
    println(cnt)
}

class CardHand(cards:List[string]) extends Ordered[cardhand] {
    val map = Map('2'->2,'3'->3,'4'->4,'5'->5,'6'->6,'7'->7,
                  '8'->8,'9'->9,'T'->10,'J'->11,'Q'->12,'K'->13,'A'->14)
    val values = cards.map{c => map(c.first)}.sort{ _ > _ }
    
    def rank :List[int] = {
        val isFlush = cards.forall{ _.last == cards.first.last }
        val isStraight = values.zipWithIndex.forall{ case(x,y) => x + y == values.first }
        if(isFlush && isStraight) return 9::values
        if(isFlush) return 6::values
        if(isStraight) return 5::values
        
        val lst = values.map{ v =>(values.filter{ _==v }.size,v) }.removeDuplicates.sort{ _ > _ }
        val vst = lst.map{ _._2 }
        if(lst.first._1 == 4) return 8::vst
        if(lst.first._1 == 3) return if(lst(1)._1 == 2) 7::vst else 4::vst
        if(lst.first._1 == 2) return if(lst(1)._1 == 2) 3::vst else 2::vst
        1::values
    }
    
    def compare(that:CardHand):Int = rank compare that.rank
}
```



[问题55](http://projecteuler.net/index.php?section=problems&id=55)：<span style="color:orange">	
How many Lychrel numbers are there below ten-thousand?</span>
<span style="color:red">题目简介</span>：首先介绍一个数学名词：[利克瑞尔数](http://baike.baidu.com/view/1826440.htm)（Lychrel Number）：<span style="color:olive">指的是将该数与将该数各数位逆序翻转后形成的新数相加、并将此过程反复迭代后，结果永远无法是一个回文数的自然数。</span>
　　现在已知对于10000以内的自然数，要么能够在50步（指将这个数与其逆序后的数相加的过程）内得到一个回文数；要么该数是一个利克瑞尔数。
　　求：10000以内利克瑞尔数的个数
<span style="color:red">答　　案</span>：249

```java
import java.math.BigInteger
object Euler055 extends Application {
    var res = 0
    for(n <- 1 until 10000){
        var b:BigInt = n
        var s = b.toString
        var r = s.reverse
        var k = 0
        do{
            b = b + BigInt(r)
            s = b.toString
            r = s.reverse
            k += 1
        }while(!s.sameElements(r) && k <= 50)
        if(k > 50) res += 1
    }
    println(res)
}
```



[问题56](http://projecteuler.net/index.php?section=problems&id=56)：<span style="color:orange">Considering natural numbers of the form, a^b, finding the maximum digital sum.</span>
<span style="color:red">题目简介</span>：记f(x)表示自然数x的各位数字之和，a^b表示a的b次方。对1≤a,b <100，求f(a^b)的最大值。
<span style="color:red">答　　案</span>：972
```java
//Scala
    val nums = for(a <- 1 to 100;b <- 1 to 100) yield (a:BigInt).pow(b)
    nums.map{ _.toString.foldLeft(0){ _+_-'0' } }.foldLeft(0){ _ max _ }
```



[问题57](http://projecteuler.net/index.php?section=problems&id=57)：<span style="color:orange">Investigate the expansion of the continued fraction for the square root of two.</span>
<span style="color:red">题目简介</span>：2的平方根有连分数的表示：√2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...
　　展开其中的前四项为：
<span style="color:olive">1 + 1/2 = 3/2 = 1.5
1 + 1/(2 + 1/2) = 7/5 = 1.4
1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666...
1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379...</span>
　　接下来的三项依次为：<span style="color:olive">99/70, 239/169,577/408</span>
　　而第八项是<span style="color:olive">1393/985</span>，注意，这是第一个出现分子位数超过分母位数的项(分子是4位数，而分母是3位数)
　　现在要求前1000项中，分子位数比分母位数多的个数。
<span style="color:red">答　　案</span>：153

```java
object Euler057 extends Application {
    var res = 0
    var a = BigInt(1)
    var b = BigInt(1)
    for(n <- 1 to 1000){
        a = a + b + b
        b = a - b
        if(a.toString.length > b.toString.length) res += 1
    }
    println(res)
}
```



[问题58](http://projecteuler.net/index.php?section=problems&id=58)：<span style="color:orange">	
Investigate the number of primes that lie on the diagonals of the spiral grid.</span>
<span style="color:red">题目简介</span>：将自然数按如下方式逆时针排列：
　　　　　　<span style="color:red">37</span> 36 35 34 33 32 <span style="color:red">31</span>
　　　　　　38 <span style="color:red">17</span> 16 15 14 <span style="color:red">13</span> 30
　　　　　　39 18 <span style="color:red">05</span> 04 <span style="color:red">03</span> 12 29
　　　　　　40 19 06 01 02 11 28
　　　　　　41 20 <span style="color:red">07</span> 08 09 10 27
　　　　　　42 21 22 23 24 25 26
　　　　　　<span style="color:red">43</span> 44 45 46 47 48 49
　　其中标红色的表示出现在对角线上且为素数的自然数，注意对角线上一共有13个自然数，其中8个为素数。也就是说素数所占的比例为 8/13 ≈ 62%.
　　将上述方阵继续排列下去，求使得对角线上素数比例小于10%的方阵的最小边长。
<span style="color:red">答　　案</span>：26241

```java
object Euler058 extends Application {
    var ps:Stream[int] = Stream.cons(2,
                             Stream.from(3).filter{ n =>
                                 ps.takeWhile(p => p*p <= n).forall(n%_ !=0)
                             })
    def isPrime(n:Int) = ps.takeWhile{ p => p*p<=n }.forall{ n%_ !=0 }
    
    val rate = 0.1
    var count = 1
    var primes = 0
    var size = 1
    var diag = 1
    do{
        size += 2
        for(loop <- 1 to 4){
            diag = diag + size -1
            if(isPrime(diag)) primes += 1
        }
        count += 4
    }while(primes >= count*rate)
    println(size)
}
```



[问题59](http://projecteuler.net/index.php?section=problems&id=59)：<span style="color:orange">	
Using a brute force attack, can you decrypt the cipher using XOR encryption?</span>
<span style="color:red">题目简介</span>：使用暴力破解一段使用异或方式加密的英文文本。
<span style="color:red">答　　案</span>：107359

```java
import scala.io.Source._
object Euler059 extends Application {
    def isLetter(c:Int):Boolean = 
        " !\"'(),-.0123456789:;?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".indexOf(c) >= 0 
        
    var src = fromFile("cipher1.txt").getLine(1).split(",").map{ _.trim.toInt.toChar }.zipWithIndex
    var pw = for(idx <- 0 until 3)
                 yield 'a'.to('z').find{ c => src.filter{ _._2%3 == idx }.forall{ s => isLetter(s._1^c) } }.get
    var sum = src.foldLeft(0){ (n,s) => n + (s._1^pw(s._2%3)) }
    println(sum)
}
```



[问题60](http://projecteuler.net/index.php?section=problems&id=60)：<span style="color:orange">Find a set of five primes for which any two primes concatenate to produce another prime.</span>
<span style="color:red">题目简介</span>：自然数3, 7, 109, 673有着非常奇特的性质：
　　　1.它们都是素数
　　　2.它们任意两个连接而成的数也是素数。比如7与3相连得到73是素数
　　　3.它们是满足上面两个性质并且使得和最小的一组数
　　现在要求5个素数使得其中任意两个连接得到的数也是素数，并且使这5个数之和最小。
　　<span style="color:brown">输出这5个数之和</span>
<span style="color:red">答　　案</span>：26033

```java
import eastsun.math.Util._
import java.util.Arrays.binarySearch

object Euler060 extends Application {   
    //get primes below 100000000
    val primes = getPrimes(100000000)
    
    def isPrime(n:Int) = binarySearch(primes,n) >= 0
    
    //get intersection of la and lb,store in order
    def intersect(la:List[int],lb:List[int]):List[int] = {
        var lx = la
        var ly = lb
        var ls:List[int] = Nil
        while(!(lx.isEmpty || ly.isEmpty)){
            var ix = lx.head
            var iy = ly.head
            if(ix >= iy) ly = ly.tail
            if(ix <= iy) lx = lx.tail
            if(ix == iy) ls = ix::ls
        }
        ls.reverse
    }
    
    def find(ls:List[int]):Int = {
        var min = Integer.MAX_VALUE
        def find0(lx:List[int],sum:Int,depth:Int){
            if(lx.size < 4 - depth) return
            if(depth == 3) min = primes(lx.first)+ sum
            else lx.foreach{ x =>
                    find0(intersect(lx,items(x)),sum+primes(x),depth+1)
                 }
        }
        find0(ls,0,0)
        min
    }
    
    val size = primes.findIndexOf{ _ >= 10000 }
    val items = new Array[list[int]](size)
    
    for(i <- 0 until size){
        var item = List[int]()
        for(j <- i+1 until size){
            var a = primes(i)
            var b = primes(j)
            if(isPrime((a+""+b).toInt) &&
               isPrime((b+""+a).toInt) ) item = j::item
        }
        items(i) = item.reverse
    }
    
    var res = Integer.MAX_VALUE
    items.zipWithIndex.foreach{ iti =>
        var f = find(iti._1)
        if(f < Integer.MAX_VALUE && f + primes(iti._2) < res) res = f + primes(iti._2)
    }   
    println(res)    
}
```