Vladar

happyticket

Jan 6th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 0.41 KB | None | 0 0
  1. import strutils, math
  2.  
  3. proc cut[T](num: T; x: int): int =
  4.   num div pow(10.0, x.float).int
  5.  
  6. proc nth[T](num: T; th: int): int =
  7.   cut(num, th) - 10*cut(num, th+1)
  8.  
  9. var counter = 0
  10. for i in 1_001..999_999:
  11.   let
  12.     sum_left = nth(i, 5) + nth(i, 4) + nth(i, 3)
  13.     sum_right = nth(i, 2) + nth(i, 1) + nth(i, 0)
  14.   if sum_left == sum_right:
  15.     inc counter
  16.  
  17. echo "Found $# happy tickets." % [$counter]
Advertisement
Add Comment
Please, Sign In to add comment