Project Euler解题汇总 051 ~ 060

  [color=red]注[/color]:本文代码中会使用[url=http://eastsun.iteye.com/admin/blogs/207614]按字典顺序生成所有的排列[/url]与[url=http://eastsun.iteye.com/admin/blogs/240339]筛法求素数[/url]中介绍的函数。

[url=http://projecteuler.net/index.php?section=problems&id=51]问题51[/url]:[color=orange]Find the smallest prime which, by changing the same part of the number, can form eight different primes.[/color]
[color=red]题目简介[/color]:对于数字56**3(其中*表示占位符),将其中的两个*换成0~9中的数字,产生的10个数字中为素数的有:56003, 56113, 56333, 56443, 56663, 56773, 56993。[color=olive]56003[/color]是这一系列素数中最小的那个。
  现在要求满足下列条件最小的那个素数:该数是由将某个数中的几位(不一定相邻)替换为相同数字而产生的8个素数中的一个。([color=red]注意[/color]:如果被替换的位置中有首位,则不能用0来替换)
[color=red]答  案[/color]:121313
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)
}



[url=http://projecteuler.net/index.php?section=problems&id=52]问题52[/url]:[color=orange]Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits in some order.[/color]
[color=red]题目简介[/color]:求最小的x,使得x的2倍,3倍,4倍,5倍,6倍都是由相同的数字组成
[color=red]答  案[/color]:142857
PS:对1/7的循环节印象比较深刻,直接就写出来了,果然是对的:-)


[url=http://projecteuler.net/index.php?section=problems&id=53]问题53[/url]:[color=orange]How many values of C(n,r), for 1 ≤ n ≤ 100, exceed one-million?[/color]
[color=red]题目简介[/color]:求组合数C(n,r) (1 ≤ n ≤ 100)中超过1000000的有多少个。
[color=red]答  案[/color]:4075
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)
}



[url=http://projecteuler.net/index.php?section=problems&id=54]问题54[/url]:[color=orange]How many hands did player one win in the game of poker?[/color]
[color=red]题目简介[/color]:一个扑克牌游戏,判断提供的数据中玩家1赢的盘数有多少次
[color=red]答  案[/color]:376
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
}



[url=http://projecteuler.net/index.php?section=problems&id=55]问题55[/url]:[color=orange]
How many Lychrel numbers are there below ten-thousand?[/color]
[color=red]题目简介[/color]:首先介绍一个数学名词:[url=http://baike.baidu.com/view/1826440.htm]利克瑞尔数[/url](Lychrel Number):[color=olive]指的是将该数与将该数各数位逆序翻转后形成的新数相加、并将此过程反复迭代后,结果永远无法是一个回文数的自然数。[/color]
  现在已知对于10000以内的自然数,要么能够在50步(指将这个数与其逆序后的数相加的过程)内得到一个回文数;要么该数是一个利克瑞尔数。
  求:10000以内利克瑞尔数的个数
[color=red]答  案[/color]:249
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)
}



[url=http://projecteuler.net/index.php?section=problems&id=56]问题56[/url]:[color=orange]Considering natural numbers of the form, a^b, finding the maximum digital sum.[/color]
[color=red]题目简介[/color]:记f(x)表示自然数x的各位数字之和,a^b表示a的b次方。对1≤a,b <100,求f(a^b)的最大值。
[color=red]答  案[/color]:972
//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 _ }



[url=http://projecteuler.net/index.php?section=problems&id=57]问题57[/url]:[color=orange]Investigate the expansion of the continued fraction for the square root of two.[/color]
[color=red]题目简介[/color]:2的平方根有连分数的表示:√2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...
  展开其中的前四项为:
[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...[/color]
  接下来的三项依次为:[color=olive]99/70, 239/169,577/408[/color]
  而第八项是[color=olive]1393/985[/color],注意,这是第一个出现分子位数超过分母位数的项(分子是4位数,而分母是3位数)
  现在要求前1000项中,分子位数比分母位数多的个数。
[color=red]答  案[/color]:153
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)
}



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



[url=http://projecteuler.net/index.php?section=problems&id=59]问题59[/url]:[color=orange]
Using a brute force attack, can you decrypt the cipher using XOR encryption?[/color]
[color=red]题目简介[/color]:使用暴力破解一段使用异或方式加密的英文文本。
[color=red]答  案[/color]:107359
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)
}



[url=http://projecteuler.net/index.php?section=problems&id=60]问题60[/url]:[color=orange]Find a set of five primes for which any two primes concatenate to produce another prime.[/color]
[color=red]题目简介[/color]:自然数3, 7, 109, 673有着非常奇特的性质:
   1.它们都是素数
   2.它们任意两个连接而成的数也是素数。比如7与3相连得到73是素数
   3.它们是满足上面两个性质并且使得和最小的一组数
  现在要求5个素数使得其中任意两个连接得到的数也是素数,并且使这5个数之和最小。
  [color=brown]输出这5个数之和[/color]
[color=red]答  案[/color]:26033
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)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值