Skip to content

Instantly share code, notes, and snippets.

@gom
Created January 25, 2016 08:10
Show Gist options
  • Save gom/be18111713d1184ec387 to your computer and use it in GitHub Desktop.
Save gom/be18111713d1184ec387 to your computer and use it in GitHub Desktop.
import math
def count_seven(n):
c = 0
for i in range(n+1):
c += str(i).count("7")
return c
def count_seven_digit_num(num):
digit_num = len(str(num)) - 1
top = int(num / math.pow(10, digit_num))
mod = int(num % math.pow(10, digit_num))
cnt = top * digit_num * math.pow(10, digit_num - 1)
if top > 7:
cnt += math.pow(10, digit_num)
elif top == 7:
cnt += num - 7 * math.pow(10, digit_num) + 1
if mod <= 0:
return int(cnt)
return int(cnt + count_seven_digit_num(mod))
n = [99, 77777, 23678947, 732465890, 1912478368]
for i in n:
print(count_seven_digit_num(i))
#print()
#print(count_seven(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment