首页 > 技术文章 > 计算1+11+111+1111+........

blythe 2017-08-16 19:18 原文

f(n)=f(n-1)+10^n;

 public static void main(String[] args) {
        int n=2017;
        long temp=1;
        for(int i=1;i<n;i++){
            temp+=func(i);
        }
        System.out.println(temp);
    }

    private static long func(int i) {
        // TODO Auto-generated method stub
        long sum;
        if(i==0){
            sum=1;
        }
        else{
            sum=(long) (Math.pow(10, i)+func(i-1));    
        }
        return sum;
    }

推荐阅读