Reputation: 427
Consider 13-digit numbers in 13-decimal numeral system (0,1,2,3,4,5,6,7,8,9,A,B,C) with leading zeros:
ABA98859978C0, 6789110551234, 0000007000000
Consider a number lucky if sum of it's first 6 digits == sum of it's last 6 digits
0055237050A00 - lucky => 0+0+5+5+2+3 = 0+5+0+A+0+0
1234AB988BABA - unlucky => 1+2+3+4+A+B != 8+8+B+A+B+A
Task: Write a program that'd count amount of all 13-digit lucky numbers in 13-decimal numeral system
What would be the way to solve this? I can only think about going from
BigInteger.Parse("-13131313131313131313131313") (that would be minimal possible value = -CCCCCCCCCCCCC)
to
BigInteger.Parse("13131313131313131313131313") (that would be maximal possible value = CCCCCCCCCCCCC)
in a loop and checking whether or not the criteria meets, but that's probably not optimal
Upvotes: 1
Views: 315
Reputation: 24427
Iterate through all possible 6-digit numbers and make a count of how many numbers add up to each possible sum. You can store this in an array with 73 elements ((6 x 12) + 1). For each sum n with m numbers adding to n, there will be m x m x 13 lucky 13-digit numbers where the first and last 6 digits both sum to n (each of the m numbers can be in the first 6 digits in combination with each of the m numbers in the last 6 digits, and the 7th digit can be anything). Sum up the total for each possible sum and that's your overall total.
Upvotes: 2