我刚刚开始使用python(python3),因为我读了它对euler项目的好处,因为它可以处理非常大的数字.
现在我正在努力解决将float转换为int的一个非常简单的问题.为什么我没有得到相同的结果:
num = 6008514751432349174082765599289028910605977570
print('num {0} '.format(int(num)))
num = num / 2
print('num /2 {0} '.format(int(num)))
num = num * 2
print('num *2 {0} '.format(int(num)))
Run Code Online (Sandbox Code Playgroud)
输出为:
num 6008514751432349174082765599289028910605977570
num /2 3004257375716174771611310192874715313222975488
num *2 6008514751432349543222620385749430626445950976
Run Code Online (Sandbox Code Playgroud) 我正在用 Java 编写一些代码,并且正在处理非常大的整数。我正在使用 BigInteger 来获取我的结果,并且我正在寻找某种方法来用逗号分隔我的结果,以便一切看起来都漂亮整洁。下面是一个简单的例子,我希望你们帮助我
import java.math.*;
public static void main(String []args){
BigInteger integer = BigInteger.valueOf(60000);
String result = integer.toString();
System.out.printf("%,8d%n",integer);
}
Run Code Online (Sandbox Code Playgroud)
我不确定在这里真正去哪里。有帮助吗?它似乎不像向字符串添加逗号那么简单,因为 BigInteger 不能直接转换为字符串,而是可以成为字符串的一部分。
我知道有很多帖子用于将字符转换为整数,字符串转换为BigInteger-s,int-s转换为BigInteger-s,...但我无法弄清楚为什么这不起作用.
Scanner sc = new Scanner(System.in);
BigInteger sum = 0;
String line = "";
while (sc.hasNext()) {
line = sc.next();
for (char character: vrstica.toCharArray()) {
sum = sum.add(BigInteger.valueOf(Character.getNumericValue(character)));
}
}
Run Code Online (Sandbox Code Playgroud)
我有Scanner和BigInteger进口.输入数据由带数字的行构成,如下所示:7218904932283439201 \n7218904932283439201 ...
如果我理解正确的话,BigInteger-s的附加内容应该是这样编写的:bigInteger1.add(bigInteger2)其中两个数字都是类型BigInteger.所以我应该转换类型的该字符char输入int,然后是转换int价值BigInteger与方法BigInteger.valueOf(),它接受一个int参数.
我得到的错误如下: incompatible types: int cannot be converted to BigInteger
我没有看到我可能出错的地方,所以如果有人能指出我的错误,我会很感激.
我正在尝试使用一些更大的整数值,但我在初始化BigInteger变量时遇到了一些问题.我一直在做:
BigInteger x = new BigInteger("" + (Math.pow(2, n)));
Run Code Online (Sandbox Code Playgroud)
其中n是100s中的某个数字,但这会引发NumberFormatException.我不认为我可以使用BigInteger,Valueof(),因为这需要很长时间,我认为不够大.任何帮助,将不胜感激.
BigInteger的JavaDoc让我觉得很不安全,例如下面的构造函数说:
BigInteger(int bitLength, int certainty, Random rnd)
Run Code Online (Sandbox Code Playgroud)
构造一个随机生成的正 BigInteger,它可能是质数,具有指定的 bitLength。
为什么只有大概?为什么不肯定?我还能相信结果是质数吗?
我正在处理BigInteger每个数字的大小为2 ^ 100的一些问题。我需要该数字的第n个数字。我该怎么做?
使用toString()我将BigInteger转换为String,然后获取该数字,但是String的大小最多仅为Int的最大值?
int get(BigInteger b,BigInteger n)
{
return Character.getNumericValue(b.toString().charAt(n.intValue()));
}
Run Code Online (Sandbox Code Playgroud)
因此,此代码仅在BigInteger小于Int max值时才有效。但是在我的情况下,经过某些迭代后,可能是我的BigInteger交叉限制的机会,因此如何在该BigInteger中获得第n个BigInteger数字?
我正在尝试生成所有已知的perfect numbers使用Euclid–Euler theorem,
我想知道我是否可以修改/重写我的代码以快速获得结果。
这是我的代码:
public static BigInteger[] genAllPerfect(int howMany)
{
int[] expn = { 2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689,
9941, 11213, 19937, 21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, 1257787, 1398269,
2976221, 3021377, 6972593, 13466917, 20996011, 24036583, 25964951, 30402457, 32582657, 37156667, 42643801,
43112609, 57885161, 74207281, 77232917, 82589933 };
BigInteger[] perfectNums = …Run Code Online (Sandbox Code Playgroud) 我相信在 64 位 JVM 中,long[] 比 int[] 效率更高,并且可以显着加快 RSA 操作的速度。
进位是 long[] 中的一个问题,但是我们可以使用一些本地方法强制 long 为无符号,例如long z = u64add(x, y, cr),这里cr是 a boolean[],可以替换long z = x + y。
在Java和Javascript 中,BigInteger都有一个被调用的函数toString(int radix),它返回给定String的 thisBigInteger的表示radix。
我想知道C# ( .NET ) 中是否有任何方法可以完成 java 和 javascript 可以做的工作?
我通过使用字符串(class )和整数(class )解决了以下leet代码问题https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/。我认为整数解决方案应该更快并且使用更少的内存。但实际上需要更长的时间。当 n=18 和 k=200 时,需要 10.1 秒,而字符串解决方案需要 0.13 秒。SolutionSolution2
import time
class Solution:
def findKthBit(self, n: int, k: int) -> str:
i = 0
Sn = "0"
Sn = self.findR(Sn, i, n)
return Sn[k-1]
def findR(self, Sn, i, n):
if i == n:
return Sn
newSn = self.calcNewSn(Sn, i)
return self.findR(newSn, i+1, n)
def calcNewSn(self, Sn, i):
inverted = ""
for c in Sn:
inverted += "1" if c == "0" else "0"
newSn …Run Code Online (Sandbox Code Playgroud) biginteger ×10
java ×7
python ×2
algorithm ×1
c# ×1
carryflag ×1
comma ×1
data-loss ×1
long-integer ×1
performance ×1
python-3.x ×1
radix ×1