Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # algoritm of calculate count of numbers in
- # row like 1, 12, 123, 1234, .. 123456789, 12345678910, 1234567891011.. N
- # thats mod 3
- import time
- i = int(input('input N: '))
- number = 0
- counter = 0
- t1 = time.monotonic()
- for i in range(1, i+1):
- # detect rank of iteration number
- rank = 1
- buf_i = i
- while True:
- if buf_i // 10:
- rank += 1
- buf_i = buf_i // 10
- else:
- break
- number = number*(10**rank) + i
- # calculate sum of digits and check it mod 3
- x = number
- x = (x >> 16) + (x & 0xffff)
- x = (x >> 10) + (x & 0x3ff)
- x = (x >> 6) + (x & 0x3f)
- x = (x >> 4) + (x & 0xf)
- while x >3:
- x = (x >> 2) + (x & 0x3)
- if (x == 3):
- counter += 1
- # old style
- '''
- num = number
- summ = 0
- while num:
- num, mod = divmod(num, 10)
- summ += mod
- if summ % 3 == 0:
- counter += 1
- '''
- t2 = time.monotonic()
- delta = t2-t1
- print('%.6fs' % delta, counter)
- # input N: 1000
- # 1.653000s 666
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement