biginteger

Performance of MutableBigInteger

北战南征 提交于 2019-12-23 03:28:55
问题 I tried calculating the sum of the digits of square root of integers below a particular input with large precison (upto 10000) using BigInteger. public class SquareRootHackerRankJarvis { static BigInteger limit; static BigInteger a; static BigInteger b; private static BigInteger squareroot(int n, BigInteger ten, BigInteger hundred, BigInteger five) { a = BigInteger.valueOf(n * 5); b = BigInteger.valueOf(5); while (b.compareTo(limit) == -1) { if (a.compareTo(b) != -1) { a = a.subtract(b); b =

How do I make BigInteger see the binary representation of this Hex string correctly?

旧时模样 提交于 2019-12-23 03:05:29
问题 The problem I have a byte[] that is converted to a hex string, and then that string is parsed like this BigInteger.Parse(thatString,NumberSyles.Hexnumber) . This seems wasteful since BigInteger is able to accept a byte[], as long as the two's complement is accounted for. An working (inefficient) example According to MSDN the most significant bit of the last byte should be zero in order for the following hex number be a positive one. The following is an example of a hex number that has this

Big Integer addition, I know the theory… still rusty in practice

烂漫一生 提交于 2019-12-22 10:45:37
问题 so, I'm trying to build a simple big integer class, I've read some pages on the internet and all that, but I'm stuck. I know the theory and I know that I need a carry but all examples I've seen, they focuded more in chars and in base 10 and well, I'm using a different approach to make it a bit more faster. I would appreciate some help with the plus assignment operator, the rest of it I'll try to figure it out by myself. #include <iostream> #include <string> #include <vector> using std::cout;

Golang Big Integers to Binary String

夙愿已清 提交于 2019-12-22 08:51:02
问题 I see it's simple to convert golang's big int (math/big package) to a string, but is there any straightforward way of converting a big int to a binary string? 回答1: Should be as easy as this: i := big.NewInt(2014) s := fmt.Sprintf("%b", i) // 11111011110 fmt.Println(s) Hope this is what you are looking for. 来源: https://stackoverflow.com/questions/23317336/golang-big-integers-to-binary-string

Recursive function calculating factorials leads to stack overflow

▼魔方 西西 提交于 2019-12-22 03:50:31
问题 I tried a recursive factorial algorithm in Rust. I use this version of the compiler: rustc 1.12.0 (3191fbae9 2016-09-23) cargo 0.13.0-nightly (109cb7c 2016-08-19) Code: extern crate num_bigint; extern crate num_traits; use num_bigint::{BigUint, ToBigUint}; use num_traits::One; fn factorial(num: u64) -> BigUint { let current: BigUint = num.to_biguint().unwrap(); if num <= 1 { return One::one(); } return current * factorial(num - 1); } fn main() { let num: u64 = 100000; println!("Factorial {}!

Hex to int C# with VERY big numbers

邮差的信 提交于 2019-12-21 07:05:27
问题 I have a 256 chars long string that contains a hex value: EC851A69B8ACD843164E10CFF70CF9E86DC2FEE3CF6F374B43C854E3342A2F1AC3E30C741CC41E679DF6D07CE6FA3A66083EC9B8C8BF3AF05D8BDBB0AA6CB3EF8C5BAA2A5E531BA9E28592F99E0FE4F95169A6C63F635D0197E325C5EC76219B907E4EBDCD401FB1986E4E3CA661FF73E7E2B8FD9988E753B7042B2BBCA76679 I want to convert it to a string with numbers like this:

BigInteger.toByteArray() returns purposeful leading zeros?

拟墨画扇 提交于 2019-12-21 05:23:10
问题 I'm transforming bigints into binary, radix16 and radix64 encoding and seeing mysterious msb zero paddings. Is this a biginteger problem that I can workaround by stripping zero padding or perhaps doing something else? My test code: String s; System.out.printf( "%s length %d\n", s = "123456789A", (new BigInteger( s, 16 )).toByteArray().length ); System.out.printf( "%s length %d\n", s = "F23456789A", (new BigInteger( s, 16 )).toByteArray().length ); Produces output: 123456789A length 5

An efficient algorithm to calculate the integer square root (isqrt) of arbitrarily large integers

﹥>﹥吖頭↗ 提交于 2019-12-21 05:05:10
问题 Notice For a solution in Erlang or C / C++ , go to Trial 4 below. Wikipedia Articles Integer square root The definition of "integer square root" could be found here Methods of computing square roots An algorithm that does "bit magic" could be found here [ Trial 1 : Using Library Function ] Code isqrt(N) when erlang:is_integer(N), N >= 0 -> erlang:trunc(math:sqrt(N)). Problem This implementation uses the sqrt() function from the C library, so it does not work with arbitrarily large integers

BigInteger -> byte[] -> Biginteger. Looks equal but if statement fails

别等时光非礼了梦想. 提交于 2019-12-20 07:49:04
问题 I am playing around with an idea I have for storing a public key for myself. For this I would need to transform the BigInteger in some sort of a variable and then recreate the BigInteger from that value. I have searched through Stackoverflow to find the best way to do this is with byte[]. This is my code in Eclipse: import java.math.BigInteger; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.interfaces.RSAPublicKey; public class Vaja2 { public static