我需要比当前普通Python长乘法更快的算法.
我试图找到一个体面的Karatsuba实现,但我不能.
def main():
a=long(raw_input())
if(a<0):
a=a*-1
a=((a*(a+1)/2)-1)
print(-a)
else:
a=(a*(a+1))/2
print(a)
main()
Run Code Online (Sandbox Code Playgroud)
如你所见,它并不复杂,只需几次乘法.但它必须在2.5秒内处理高达100000位的数字.
我想要一些函数片段,或者只是一个快速乘法函数实现的链接,或任何有用的东西.
我认为完全转换为BigInteger []在我的情况下是最佳的.有没有人用Java编写并发现这个并愿意分享?
所以想象一下,我有任意大小byte[] = {0xff,0x3e,0x12,0x45,0x1d,0x11,0x2a,0x80,0x81,0x45,0x1d,0x11,0x2a,0x80,0x81}
如何将其转换为BigInteger数组,然后能够安全地将其恢复回原始字节数组?
ty提前.
在x86上实现bignums时,显然数字大小的最有效选择是32位.但是,您需要算术最多两倍的数字大小(即32 + 32 = 33,32*32 = 64,64/32 = 32).幸运的是,x86不仅提供了这一功能,而且还可以从便携式C(uint64_t)访问它.
类似地,在x64上,希望使用64位数字.这将需要128位算术(即64 + 64 = 65,64*64 = 128,128/64 = 64).幸运的是,x64提供了这个功能.不幸的是,它无法通过便携式C接入,但很明显可以进入组装.
所以我的问题是它是否可从非便携式C访问.X64上的任何C编译器是否提供对此的访问,如果是,那么语法是什么?
(注意,我不是在谈论128位向量,它们被严格地视为32或64位字的集合,它们之间没有进位传播,但是关于实际的128位整数运算.)
示例(星号旁边的星号):
[Fact]
public void BigInteger_ToString_behavior_is_odd()
{
writeHex(new BigInteger(short.MaxValue)); // 7fff
writeHex(new BigInteger(short.MaxValue) + 1); // 08000 **
writeHex(new BigInteger(ushort.MaxValue)); // 0ffff **
writeHex(new BigInteger(ushort.MaxValue) + 1); // 10000
writeHex(new BigInteger(int.MaxValue)); // 7fffffff
writeHex(new BigInteger(int.MaxValue) + 1); // 080000000 **
writeHex(new BigInteger(uint.MaxValue)); // 0ffffffff **
writeHex(new BigInteger(uint.MaxValue) + 1); // 100000000
writeHex(new BigInteger(long.MaxValue)); // 7fffffffffffffff
writeHex(new BigInteger(long.MaxValue) + 1); // 08000000000000000 **
writeHex(new BigInteger(ulong.MaxValue)); // 0ffffffffffffffff **
writeHex(new BigInteger(ulong.MaxValue) + 1); // 10000000000000000
}
private static void writeHex(BigInteger value)
{
Console.WriteLine(value.ToString("x")); …Run Code Online (Sandbox Code Playgroud) SQL 2005,600,000,000行.
我有一个名为Location的表,当前在标识PK列LocationID中使用数据类型INT.我想尝试将此数据类型转换为BIGINT.
我认为以下脚本应该有助于允许插入PK列,但我不确定如何在此处进行表单.
SET IDENTITY_INSERT LOCATION ON /*allows insert into the identity column*/`
SET IDENTITY_INSERT LOCATION OFF /*Returns the identity column to initial state*/`
Run Code Online (Sandbox Code Playgroud)
位置表创建脚本如下:
CREATE TABLE [dbo].[Location](
[LocationID] [int] IDENTITY(1,1) NOT NULL,
[JourneyID] [int] NULL,
[DeviceID] [int] NOT NULL,
[PacketTypeID] [int] NULL,
[PacketStatusID] [int] NULL,
CONSTRAINT [Location_PK] PRIMARY KEY CLUSTERED
(
[LocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Location] …Run Code Online (Sandbox Code Playgroud) 我正在处理BigInteger类,其数字大约为2,上升到10,000,000.
BigInteger Log函数现在是我算法中最昂贵的函数,我正在拼命寻找替代方案.
因为我只需要日志的组成部分,所以我遇到了这个答案,这在速度方面看起来很棒,但由于某些原因我没有得到准确的值.我不关心小数部分,但我确实需要得到一个准确的积分部分,无论该值是浮动还是上限,只要我知道哪个.
这是我实现的功能:
public static double LogBase2 (System.Numerics.BigInteger number)
{
return (LogBase2(number.ToByteArray()));
}
public static double LogBase2 (byte [] bytes)
{
// Corrected based on [ronalchn's] answer.
return (System.Math.Log(bytes [bytes.Length - 1], 2) + ((bytes.Length - 1) * 8));
}
Run Code Online (Sandbox Code Playgroud)
除角落情况外,这些值现在非常准确.值7到7.99999,15到15.9999,23到23.9999 31到31.9999等返回-Infinity.数字似乎围绕字节边界.知道这里发生了什么吗?
例:
LogBase2( 1081210289) = 30.009999999993600 != 30.000000000000000
LogBase2( 1088730701) = 30.019999999613300 != 30.000000000000000
LogBase2( 2132649894) = 30.989999999389400 != 30.988684686772200
LogBase2( 2147483648) = 31.000000000000000 != -Infinity
LogBase2( 2162420578) = 31.009999999993600 != -Infinity
LogBase2( 4235837212) = …Run Code Online (Sandbox Code Playgroud) 比方说,我有一个很大的数字
BigInteger a=Biginteger.valueOf(50);
Run Code Online (Sandbox Code Playgroud)
除了以下
BigInteger a=(BigInteger.ZERO).subtract(BigInteger.valueOf(50));
Run Code Online (Sandbox Code Playgroud)
有没有另一种方法来获得我的号码的负值?似乎找不到方法.
我试图在BigInteger实例中存储一个大的计算.我试过这个:
BigInteger answer=new BigInteger(2+3);
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
temp.java:17: error: BigInteger(long) has private access in BigInteger
BigInteger answer=new BigInteger(2+3);
^
1 error
Run Code Online (Sandbox Code Playgroud)
我知道应该有一个字符串值而不是"2 + 3".但我不知道如何满足这个条件(假设我没有多少2 + 3).请告诉我如何为BigInteger对象分配计算值(将2 + 3分配给BigInteger答案).
在python中,如果我们让a = 2*4,那么"a"将是整数类型.但是如果我们让a = 2**400,那么"a"将自动为long类型,这是java的BigInteger对应物.
因此,Python可以在必要时自动将整数转换为BigInteger.我的问题是:如果每次对整数执行算术运算,Python都会检查此操作是否导致溢出.如果溢出,将其转换为BigInteger.这不是很贵吗?因为这基本上意味着Python在每个整数算术指令之后插入溢出检查指令.那么python如何自然地支持大整数并且高效?
为什么此Java代码会引发StackOverflowError异常?
public class factorial2 {
public BigInteger fact( BigInteger n)
{
BigInteger one = new BigInteger("1");
if(n.equals("0"))
return one;
else
return n.multiply(fact(n.subtract(one)));
}
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
factorial2 f = new factorial2();
for(int i=0;i<n;i++)
{
BigInteger b = sc.nextBigInteger();
System.out.println(f.fact(b));
}
sc.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用生成阶乘BigInteger。但是,为什么我的代码在输入时给出引用异常?