Created
September 9, 2012 10:29
-
-
Save arosh/3683682 to your computer and use it in GitHub Desktop.
Project Euler Problem 49
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.mutable | |
val bs = mutable.BitSet( (2 to 9999): _*) | |
var cur = 2 | |
while(cur * cur <= 9999) { | |
if(bs(cur)) { | |
var mul = 2 * cur | |
while(mul <= 9999) { | |
bs -= mul | |
mul += cur | |
} | |
} | |
cur += 1 | |
} | |
for(first <- 1000 to 9999) { | |
val stream = Stream.iterate(first) { _ + 3330 } takeWhile { _ <= 9999 } | |
if( stream.size >= 3 && stream.forall(bs.apply) ) { | |
if( stream.map { number => number.toString.foldLeft(Map.empty[Char, Int].withDefaultValue(0)) { | |
(m, c) => m.updated(c, m(c) + 1) } }.toSet.size == 1) | |
println(stream.mkString) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment