pe706img

能被3整除不用测试,只要数字组合各位和等于3的倍数
测试程序

def f(n):
  s=str(n)
  c=0
  for i in range(len(s)):
    su=int(s[i])
    if su %3 ==0:c+=1
    for j in range(i+1,len(s)):
      su+=int(s[j])
      if su %3 ==0:c+=1
  return c

def F(n):
  c=0
  for i in range(10**(n-1),10**n):
    if f(i) % 3==0: c+=1
  return c

>>> F(2)
30
>>> F(6)
290898