Skip to content

Instantly share code, notes, and snippets.

@arosh
Created September 9, 2012 10:29
Show Gist options
  • Save arosh/3683682 to your computer and use it in GitHub Desktop.
Save arosh/3683682 to your computer and use it in GitHub Desktop.
Project Euler Problem 49
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