编写程序java程序,求1+3+7+15+31+.....+(2^20-1)的值

[复制链接]
查看11 | 回复2 | 2020-6-1 23:19:41 | 显示全部楼层 |阅读模式
public class PowerTest { // 幂运算 public static long pow(long num, int exp) {long result = 1;if(exp==0){ return result;}while (exp > 1) { if ((exp & 1) != 0)
result *= num; num *= num; exp /= 2;}result = result * num;return result; } // 计算 public static long work(int n) {long result = 0;while (n != 0) { result += pow(2, n) - 1; n--;}return result; } // 测试 public static void main(String[] args) {System.err.println(work(20)); }}
回复

使用道具 举报

千问 | 2020-6-1 23:19:41 | 显示全部楼层
integer sum=0;for(integer i=1;i<math.power(2,20);i++)sum+=i
回复

使用道具 举报

千问 | 2020-6-1 23:19:41 | 显示全部楼层
public class A(){public static void main(String arg[]){
int s = 0;
for(in i=1;i<=20;i++)
sum += (2^i-1); }}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行