Skip to content

Instantly share code, notes, and snippets.

@jb77
Created August 4, 2014 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jb77/8bf0280bd1c1361c77a3 to your computer and use it in GitHub Desktop.
Save jb77/8bf0280bd1c1361c77a3 to your computer and use it in GitHub Desktop.
/*
http://richardwiseman.wordpress.com/2014/08/04/answer-to-the-friday-puzzle-269/
OK, onto the puzzle. Can you create a 10-digit number,
where the first digit is how many zeros in the number,
the second digit is how many 1s in the number etc
until the tenth digit which is how many 9s in the number.
*/
def digitSum(i:Long) = i.toString.toList.map(_.asDigit).sum
def selfReferential(number:String) : Boolean = {
val numsWithIdx=number.zipWithIndex
numsWithIdx.forall( nIdx => number.count(_.asDigit==nIdx._2)==nIdx._1.asDigit)
}
// would like to use Range, but doesn't go high enough
var i = 0L
while(i < 9999999999L) {
if(digitSum(i)==10) {
val padded="%010d".format(i)
if(selfReferential(padded)) println(padded)
}
if(i%100000000==0) print(".")
i+=1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment