Find the nth number in the increasing sequence formed by 0,2,4,6,8?

故事扮演 提交于 2019-11-30 19:21:57

The nth number in this sequence is n in base 5, with the digits doubled.

def base5(n):
    if n == 0: return
    for x in base5(n // 5): yield x
    yield n % 5

def seq(n):
    return int(''.join(str(2 * x) for x in base5(n)) or '0')

for i in xrange(100):
    print i, seq(i)

This runs in O(log n) time. I don't think it's possible to do it in O(1) time.

It can be simplified a bit by combining the doubling of the digits with the generation of the base 5 digits of n:

def seq(n):
    return 10 * seq(n // 5) + (n % 5) * 2 if n else 0
api_48
int Code()
{
    k=0;
   for(i=0;i<=10000;i++)
   {
       count=0;
       n=i;
       while(n!=0)
       {
           c=n%10;
           n=n/10;
           if(c%2!=0)
           {
               count=1;
           }
       }
       if(count==0)
       { a[k]=i;
       k++;}
    }
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!