Created
August 4, 2014 12:34
-
-
Save jb77/8bf0280bd1c1361c77a3 to your computer and use it in GitHub Desktop.
Brute Force Puzzle Solver: http://richardwiseman.wordpress.com/2014/08/04/answer-to-the-friday-puzzle-269/
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
/* | |
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