Subset a large number into smaller series of numbers using python -
i have big number lets of around hundred digits. want subset big number consecutive number of 5 digits , find product of 5 digits. example first 5 digit number 73167. need check product of individual numbers in 73167 , on.
the sample number follows:
73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557
i have problem subsetting small numbers out of big number.
my basic starting code :
b = 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 jd = str(b) ch in jd: number = ch print (number)
any highly appreciated.
edit: believe grouper
overkill in solution, @ solution @haidro https://stackoverflow.com/a/16078696/1219006
using grouper recipe itertools
i'm assuming b
string begin because crazy waste of memory make number big.
from itertools import izip_longest operator import mul def grouper(n, iterable, fillvalue=none): "collect data fixed-length chunks or blocks" # grouper(3, 'abcdefg', 'x') --> abc def gxx args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) g in grouper(5, b, fillvalue=''): # , work out product of digits num = ''.join(g) prod = reduce(mul, map(int, num))
Comments
Post a Comment