lundi 16 novembre 2015

python fastest way to generate number like 66666... when num of digits given

I have an interesting problem where I want to generate a big number (~30000 digits) but it has to be all identical digits, like 66666666666666.......

so far I have done this by

def fillWithSixes(digits):
    result = 0
    for i in range(digits):
        result *= 10
        result += 6
    return result

However, this is very inefficient, and was wondering if there is any better way? Answer in cpp or java is okay too.

Edit: 1. Let's not just solve for 666666..... I want it to be generic for any number. How about 7777777777.... or 44444........ or 55555...?

  1. String operations are worse, the increase from current complexity of O(n) to O(n^2).



Aucun commentaire:

Enregistrer un commentaire