多年来,当我为MySQL表创建一个类型为bigint(12)的新列时,我一直在假设该字段限制为最多12位的整数.但是,我最近注意到,最多16位的值可以写入bigint(12)定义列中并从中选择,而不会出现任何警告或问题.
有人可以帮助我理解为什么会出现这种情况以及该列定义实际意味着什么?提前致谢!
我需要一个具有BigInteger类型的构造函数的类.我想用这个代码将默认值设置为0,但是我遇到了编译错误.
public class Hello
{
BigInteger X {get; set;}
public Hello(BigInteger x = 0)
{
X = x;
}
}
public class MyClass
{
public static void RunSnippet()
{
var h = new Hello(); // Error <--
Run Code Online (Sandbox Code Playgroud)
怎么了?有没有办法将默认值设置为BigInteger参数?
我正在解决一个简单的组合问题,其解决方案是2 ^(n-1).
唯一的问题是1 <= n <= 2 ^ 31 -1(有符号32位整数的最大值)
我尝试使用Java的BigInteger类,但它超过了数字2 ^ 31/10 ^ 4和更高,因此显然无法解决.
此外,我仅限于使用Java或C++的内置类.
知道我需要速度,我选择在C++中构建一个对字符串进行算术运算的类.
现在,当我进行乘法时,我的程序与我们在纸上乘以效率的方式相似(而不是重复添加字符串).
但即使有了这个,我也不能将2乘以2 ^ 31 - 1倍,它只是不够有效.
所以我开始阅读有关问题的文本,我找到了...的解决方案
2^n = 2^(n/2) * 2^(n/2) * 2^(n%2) (其中/表示整数除法,%表示模数)
这意味着我可以用对数乘法来求解取幂.但对我来说,我无法解决如何将此方法应用于我的代码?如何选择下限以及跟踪最终乘法所需的各种数字的最有效方法是什么?
如果有人知道如何解决这个问题,请详细说明(示例代码表示赞赏).
UPDATE
感谢大家的帮助!很明显,这个问题意味着以一种现实的方式解决,但我确实设法胜过java.math.BigInteger仅执行ceil(log2(n))迭代的幂函数.
如果有人对我制作的代码感兴趣,这里是......
using namespace std;
bool m_greater_or_equal (string & a, string & b){ //is a greater than or equal to b?
if (a.length()!=b.length()){
return a.length()>b.length();
}
for (int i = 0;i<a.length();i++){
if (a[i]!=b[i]){
return a[i]>b[i];
}
}
return true;
}
string add (string& …Run Code Online (Sandbox Code Playgroud) 我搜索了数据库和食谱,但似乎无法找到正确的答案.我有一个非常简单的python代码,它总结了一个范围内的自我权力.我需要这个非常大的数字的最后十位数字,我已经尝试了getcontext().但是我仍然达到了极限.
这是代码:
def SelfPowers(n):
total = 0
for i in range(1,n):
total += (i**i)
return(total)
print SelfPowers(n)
Run Code Online (Sandbox Code Playgroud)
我怎么能看到所有那些美丽的数字?它在我的四核上打印速度相对较快.这只是为了ProjectEuler的乐趣,问题#48,没有剧透请求我不想解决方案而且我不想为我完成工作,所以如果你能指出我正确的方向吗?
谢谢,mp
我正在努力改变我的Fibonacci音序器,以便在达到~100周期后的数字不会回旋并变为负数.如何在我编写的代码中使用BigInteger:
package me.kevinossia.mystuff;
import java.util.Scanner;
public class FibonacciDisplayer
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int total;
System.out.print("This is a Fibonacci sequence displayer.\nHow many numbers would you like it to display?");
total = input.nextInt();
long[] series = new long[total];
series[0]=0;
series[1]=1;
for(int i=2;i<total;i++)
{
series[i]=series[i-1] + series[i-2];
}
for(int j=0; j<total; j++)
{
System.out.print(series[j] + "\n");
}
input.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我搜索谷歌的高低,我找不到任何具体到我的情况.
我正在尝试加载超过2 ^ 32个元素的数据集,并将这些元素放在ArrayList中anArrayList.此数据按时间顺序排列,因此我使用ArrayList存储数据以保持顺序.同时,我想快速访问String中的元素elementID.现在我使用HashMap将a映射elementID到元素Object中anArrayList.我使用整数currentAddingAt来跟踪anArrayList上的索引以添加元素.这是相关的代码:
ArrayList<ElementX> anArrayList;
int currentAddingAt;
HashMap<String, ElementX> elementToObjHashMap;
... ...
public void addAnElement(ElementX e){
anArrayList.add(currentAddingAt, e);
elementToObjHashMap.put(e.getElementID, ArrayList.get(currentAddingAt));
}
Run Code Online (Sandbox Code Playgroud)
当我改变的类型的问题来了currentAddingAt,从int到long.因为ArrayList的get(int index)方法仅int作为参数,根据Oracle的文档(http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html).这也让我想知道:
ArrayList的容量可以大于Java中的最大int数(2 ^ 32)吗?
除了在这种情况下使用ArrayList和HashMap之外还有哪些选项(保持大数据集的顺序并且仍然能够从键快速映射到对象)?除了普通的Java之外,我还需要一些库(甚至是某些框架)吗?
我一直在研究一个java程序,我需要在其中计算并将巨大的值存储到数组中.到目前为止,我通过将输入的变量插入到指数函数中来获取值:
Math.pow(7,x);
Run Code Online (Sandbox Code Playgroud)
我已决定将值存储到BigInteger数组中,但我不知道如何使用BigInteger提供的提供的构造函数存储这些常规操作值.对于'x'值,我实际上使用了相当大的数字,它们可以存储长度可以存储的指定值.我似乎完全围绕着我想到的每一个解决方案...简单地做:
bigArray[i] = new BigInteger((long)Math.pow(7,x));
Run Code Online (Sandbox Code Playgroud)
不起作用,因为我处理大于100的值为x.我能做什么?
我正在尝试计算一个大数,这需要BigInteger.Pow(),但我需要指数也是一个BigInteger而不是int.
即
BigInteger.Pow(BigInteger)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
编辑:我想出了一个答案.用户狗帮助我实现了这一目标.
public BigInteger Pow(BigInteger value, BigInteger exponent)
{
BigInteger originalValue = value;
while (exponent-- > 1)
value = BigInteger.Multiply(value, originalValue);
return value;
}
Run Code Online (Sandbox Code Playgroud) 以下是我的代码,我尝试使用BigInteger inn Java计算从1到100的所有数字的LCM.但它没有提供任何答案.
import java.math.BigInteger;
public class CommonOneToHundred {
public static void main(String[] args) {
// TODO Auto-generated method stub
BigInteger res =new BigInteger("1");
int i = 2;
while(i<=100){
res = lcm(res,BigInteger.valueOf(i));
i++;
}
System.out.println(res);
}
static BigInteger lcm(BigInteger x, BigInteger y)
{
BigInteger a;
//a = (x > y) ? x : y; // a is greater number
a = y;
while(true)
{
if(a.divide(x).equals(0) && a.divide(y).equals(0))
return a;
a = a.add(BigInteger.ONE);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我编写了以下教程的实现:LINK
基本上,由于C/C++没有BIG Integer,我们将阶乘十进制值存储在一个数组中.这相当于编写一个乘法,用于执行在学校教授孩子的乘法.
问题:对于高达17的值,它可以正常工作!之后(18!,19!,...)它不会输出正确的值.
#include <iostream>
using namespace std;
int main(){
int fact[1000]={1};
int n; scanf("%d", &n); //n are the number of factorials we will calculate
while(n--){
int number; scanf("%d", &number); //scan the number
if(number == 0) printf("%d", 1);
int flag = number;
int index = 0, length = 0;
//following lines we find the length of the entered number
while(flag!=0){
fact[index] = flag%10;
flag /= 10;
index++; length++;
}
//following lines are the multiplication code
while(number>1){
index = …Run Code Online (Sandbox Code Playgroud) biginteger ×10
java ×4
arrays ×2
c# ×2
c++ ×2
arraylist ×1
bigdata ×1
bignum ×1
definition ×1
factorial ×1
fibonacci ×1
large-data ×1
lcm ×1
mysql ×1
python ×1