如何找到2的力量n.n的范围是0到200

mou*_*sey 10 c c++ java

假设我的系统为32位机器.考虑到这一点,如果我使用long int为n> 63我会得到我的值为0.如何解决?

fre*_*low 14

double完全能够存储多达1023的两个大国的究竟.不要让别人告诉你浮点数在某种程度上总是不准确.这是一个特殊情况,他们不是!

double x = 1.0;
for (int n = 0; n <= 200; ++n)
{
    printf("2^%d = %.0f\n", n, x);
    x *= 2.0;
}
Run Code Online (Sandbox Code Playgroud)

该程序的一些输出:

2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
...
2^196 = 100433627766186892221372630771322662657637687111424552206336
2^197 = 200867255532373784442745261542645325315275374222849104412672
2^198 = 401734511064747568885490523085290650630550748445698208825344
2^199 = 803469022129495137770981046170581301261101496891396417650688
2^200 = 1606938044258990275541962092341162602522202993782792835301376
Run Code Online (Sandbox Code Playgroud)


pax*_*blo 7

只需等待256位编译器,然后使用int:-)

不,严重的是,因为你只想从1开始并保持加倍,你最好的选择是获得一个像GNU MP这样的大整数库.


你会用一段代码(未经测试)来做到这一点:

#include <stdio.h>
#include "gmp.h"

int main (void) {
    int i;
    mpz_t num;

    mpz_init_set_ui (num, 1);
    for (i = 0; i <= 200; i++) {
        printf ("2^%d = ", i);
        mpz_out_str (NULL, 10, num);
        printf ("\n");
        mpz_mul_ui (num, num, 2);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

您只需要两个操作,双重和打印就可以编写自己的long数组结构,但我认为使用GMP要容易得多.

如果你想推出自己的,看看这个.这是我过去开发的一些大型整数库的变化/简化:

#include <stdio.h>
#include <stdlib.h>

// Use 16-bit integer for maximum portability. You could adjust
//   these values for larger (or smaller) data types. SZ is the
//   number of segments in a number, ROLLOVER is the maximum
//   value of a segment plus one (need to be less than the
//   maximum value of your datatype divided by two. WIDTH is
//   the width for printing (number of "0" characters in
//   ROLLOVER).

#define SZ 20
#define ROLLOVER 10000
#define WIDTH 4
typedef struct {
    int data[SZ];
} tNum;
Run Code Online (Sandbox Code Playgroud)

 

// Create a number based on an integer. It allocates the segments
//   then initialises all to zero except the last - that one is
//   set to the passed-in integer.

static tNum *tNumCreate (int val) {
    int i;

    tNum *num = malloc (sizeof (tNum));
    if (num == NULL) {
        printf ("MEMORY ERROR\n");
        exit (1);
    }

    for (i = 0; i < SZ - 1; i++) {
        num->data[i] = 0;
    }
    num->data[SZ-1] = val;
}

// Destroy the number. Simple free operation.

static void tNumDestroy (tNum *num) {
    free (num);
}
Run Code Online (Sandbox Code Playgroud)

 

// Print the number. Ignores segments until the first non-zero
//   one then prints it normally. All following segments are
//   padded with zeros on the left to ensure number is correct.
//   If no segments were printed, the number is zero so we just
//   output "0". Then, no matter what, we output newline.

static void tNumPrint (tNum *num) {
    int i, first;
    for (first = 1, i = 0; i < SZ; i++) {
        if (first) {
            if (num->data[i] != 0) {
                printf ("%d", num->data[i]);
                first = 0;
            }
        } else {
            printf ("%0*d", WIDTH, num->data[i]);
        }
    }
    if (first) {
        printf ("0");
    }
    printf ("\n");
}
Run Code Online (Sandbox Code Playgroud)

 

// Double a number. Simplified form of add with carry. Carry is
//   initialised to zero then we work with the segments from right
//   to left. We double each one and add the current carry. If
//   there's overflow, we adjust for it and set carry to 1, else
//   carry is set to 0. If there's carry at the end, then we have
//   arithmetic overflow.

static void tNumDouble (tNum *num) {
    int i, carry;
    for (carry = 0, i = SZ - 1; i >= 0; i--) {
        num->data[i] = num->data[i] * 2 + carry;
        if (num->data[i] >= ROLLOVER) {
            num->data[i] -= ROLLOVER;
            carry = 1;
        } else {
            carry = 0;
        }
    }
    if (carry == 1) {
        printf ("OVERFLOW ERROR\n");
        exit (1);
    }
}
Run Code Online (Sandbox Code Playgroud)

 

// Test program to output all powers of 2^n where n is in
//   the range 0 to 200 inclusive.

int main (void) {
    int i;
    tNum *num = tNumCreate (1);
    printf ("2^  0 = ");
    tNumPrint (num);
    for (i = 1; i <= 200; i++) {
        tNumDouble (num);
        printf ("2^%3d = ", i);
        tNumPrint (num);
    }
    tNumDestroy (num);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

及其相关输出:

2^  0 = 1
2^  1 = 2
2^  2 = 4
2^  3 = 8
2^  4 = 16
2^  5 = 32
2^  6 = 64
2^  7 = 128
2^  8 = 256
2^  9 = 512
: : : : :
2^191 = 3138550867693340381917894711603833208051177722232017256448
2^192 = 6277101735386680763835789423207666416102355444464034512896
2^193 = 12554203470773361527671578846415332832204710888928069025792
2^194 = 25108406941546723055343157692830665664409421777856138051584
2^195 = 50216813883093446110686315385661331328818843555712276103168
2^196 = 100433627766186892221372630771322662657637687111424552206336
2^197 = 200867255532373784442745261542645325315275374222849104412672
2^198 = 401734511064747568885490523085290650630550748445698208825344
2^199 = 803469022129495137770981046170581301261101496891396417650688
2^200 = 1606938044258990275541962092341162602522202993782792835301376
Run Code Online (Sandbox Code Playgroud)


Tho*_*rin 6

自从我认真使用Java以来​​已经很久了,但是:BigInteger类?它具有所有常用的数学(multiply,pow)和按位(shiftLeft)运算.

你的标记有点令人困惑,你更喜欢哪种语言?


buk*_*zor 6

python支持开箱即用的大整数.在任何linux提示符下,运行以下命令:

$ python -c "for power in range(201): print power, 2**power"
0 1
1 2
2 4
3 8
4 16
5 32
6 64
<snip>
196 100433627766186892221372630771322662657637687111424552206336
197 200867255532373784442745261542645325315275374222849104412672
198 401734511064747568885490523085290650630550748445698208825344
199 803469022129495137770981046170581301261101496891396417650688
200 1606938044258990275541962092341162602522202993782792835301376
Run Code Online (Sandbox Code Playgroud)

如有必要,可以很容易地将其制作成脚本.看任何python教程.


pol*_*nts 5

使用java.math.BigInteger.shiftLeft.

    for (int i = 0; i <= 200; i++) {
        System.out.format("%d = %s%n", i, BigInteger.ONE.shiftLeft(i));
    }
Run Code Online (Sandbox Code Playgroud)

输出摘录:

0 = 1
1 = 2
2 = 4
3 = 8
4 = 16
:
197 = 200867255532373784442745261542645325315275374222849104412672
198 = 401734511064747568885490523085290650630550748445698208825344
199 = 803469022129495137770981046170581301261101496891396417650688
200 = 1606938044258990275541962092341162602522202993782792835301376
Run Code Online (Sandbox Code Playgroud)

如果BigInteger不可用,您也可以手动执行乘法并将其存储在a中String.

    String s = "1";
    for (int i = 0; i < 200; i++) {
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        for (char ch : s.toCharArray()) {
            int d = Character.digit(ch, 10) * 2 + carry;
            sb.append(d % 10);
            carry = d / 10;
        }
        if (carry != 0) sb.append(carry);
        s = sb.toString();
        System.out.format("%d = %s%n", i + 1, sb.reverse());
    }
Run Code Online (Sandbox Code Playgroud)

(见完整输出)