0

I have a big number lets say of around hundred digits. I want to subset that big number into consecutive number of 5 digits and find the product of those 5 digits. For example my first 5 digit number would be 73167. I need to check the product of the individual numbers in 73167 and so on.

The sample number is as follows:

73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557

I have a problem subsetting the small numbers out of the big number.

My basic starting code is :

b = 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557
jd = str(b)
for ch in jd:
    number = ch
    print (number)

Any help is highly appreciated.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Jd Baba
  • 5,948
  • 18
  • 62
  • 96

6 Answers6

4

Edit: I believe that grouper is overkill in this solution, look at the solution by @Haidro https://stackoverflow.com/a/16078696/1219006


Using the grouper recipe from itertools

I'm assuming b is a string to begin with because it would be a crazy waste of memory to make a number that big.

from itertools import izip_longest
from operator import mul

def grouper(n, iterable, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

for g in grouper(5, b, fillvalue=''):
    # And to work out the product of the digits
    num = ''.join(g)
    prod = reduce(mul, map(int, num))
Community
  • 1
  • 1
jamylak
  • 128,818
  • 30
  • 231
  • 230
  • Wow, that's a nice function there (the grouper one)! +1 for sure. – TerryA Apr 18 '13 at 09:29
  • @Haidro I +1d you too, I actually think in this case your method is actually more appropriate. I would rather have yours accepted, I realised `grouper` is overkill here since you already know it's a string for sure... It's good for lists and other iterables that can't be sliced but not here I think. request to OP to change accepted answer! – jamylak Apr 18 '13 at 09:38
  • @jamylak It's fine :). OP accepted yours, so he/she's probably finished looking at this question. – TerryA Apr 18 '13 at 09:42
  • 1
    @Haidro Added a link to your answer since I prefer it for this scenario – jamylak Apr 18 '13 at 09:45
3

Try this:

from operator import mul
def chunker(seq, size):
    return (seq[pos:pos + size] for pos in xrange(0, len(seq), size))
for i in chunker(str(myint),5): # Where myint is that big number
    reduce(mul, map(int, i))
TerryA
  • 58,805
  • 11
  • 114
  • 143
  • 1
    I would recommend `from operator import mul`, then your reduce can be `reduce(mul, map(int, i))` – jamylak Apr 18 '13 at 09:48
2

one line for you:

import re; re.findall("\d{5}", number)             
0x90
  • 39,472
  • 36
  • 165
  • 245
  • I've corrected the syntax error your original regex had, and also changed it into a real one-liner. –  Apr 18 '13 at 08:47
  • @Evert imports aren't included. I approved the edit thanks :) – 0x90 Apr 18 '13 at 08:48
  • Of course `import`s are included; otherwise you get `NameError`s. And yes, I am being pedantic. –  Apr 18 '13 at 08:55
  • no included in the counting. that is a useless argument though – 0x90 Apr 18 '13 at 08:59
2

My take on it:

import re
from operator import mul

print [reduce(mul, map(int, group)) for group in re.findall(r'\d{5}', str(b))]
# [882, 630, 0, 648, 20, 6048, 1680, 840, 540, 3888, 11664, 0, 1960, 0, 1890, 0, 1728, 0, 16128, 480, 1920, 0, 162, 6480, 0, 1323, 360, 3600, 0, 0, 0, 12096, 1400, 864, 0, 1620, 0, 360, 0, 2100]
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
1

The first option is you can convert this number to the string type, make text processing and convert back to numbers. The second option, you can divide this number on 10000 and save the fractional part, then repeat this procedure to the floor part of the number.

freude
  • 3,632
  • 3
  • 32
  • 51
  • ok, then this post should be useful: http://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python – freude Apr 18 '13 at 08:50
0

I came up with my own version of answer. It may not as concise as other codes posted here.

# Read values
b = 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557
jd = str(b)
max = 0
for i in range(0,len(jd)):
    number = jd[i:i+3]
    if len(number)==3:
        product = int(str(number)[0])*int(str(number)[1])*int(str(number)[2])
        if product > max:
            max = product
        else:
            max = max

print (max)
Jd Baba
  • 5,948
  • 18
  • 62
  • 96