Questions tagged [integer-arithmetic]

Anything related to integer arithmetic, i.e. arithmetic operations on integer numbers. This is mostly relevant for languages which represent integer numbers with specific data-types (e.g. `int` or `long` in C, C++ or Java).

Anything related to integer arithmetic, i.e. arithmetic operations on integer numbers. This is mostly relevant for languages which represent integer numbers with specific data-types (e.g. int or long in C, C++ or Java).

381 questions
18
votes
1 answer

What's special about 787?

In ghci, using the arithmoi package: Math.NumberTheory.Powers.General> :set +s Math.NumberTheory.Powers.General> integerRoot 786 ((10^32)^786) 100000000000000000000000000000000 (0.04 secs, 227,064 bytes) Math.NumberTheory.Powers.General> integerRoot…
Daniel Wagner
  • 128,625
  • 9
  • 198
  • 347
18
votes
3 answers

Why do different algorithms of summing not match?

Assume that I want to get sum of all squares from M to N. I googled a bit and found this formula: (1^2 + 2^2 + 3^2 + ... + N^2) = (N * (N + 1) * (2N + 1)) / 6 so I write this code: static void Main(string[] args) { const int from = 10; …
Alex Zhukovskiy
  • 8,186
  • 2
  • 51
  • 123
16
votes
3 answers

Efficiently detect that rational numbers are equal

I have a collection of many rational numbers, with the numerator and denominator of each stored as a large (hundreds or thousands of bits) unsigned integer. I'd like to be able to efficiently test whether any given rational number a/b in the set is…
Sneftel
  • 34,359
  • 11
  • 60
  • 94
15
votes
8 answers

How do you store an arbitrarily large integer value in memory?

I have to store an integer value that is larger than the maximum value for the long datatype. How would I store and manipulate this value in memory? Please illustrate it through an example, if possible.
yatin
  • 177
  • 1
  • 1
  • 3
12
votes
17 answers

Subtraction without minus sign in C

How can I subtract two integers in C without the - operator?
SyncMaster
  • 8,624
  • 29
  • 84
  • 124
12
votes
2 answers

Could type punning signed to unsigned integers make bounds checking faster by eliminating the need for >= comparison?

Say I had a really performance-critical loop in my program where I need to check if a point was inside a rectangle, but I know at compile time that the lower bounds are always going to be 0, like the following: (x >= 0 && y >= 0 && x < width && y <…
Stuntddude
  • 212
  • 1
  • 15
11
votes
1 answer

Why does divMod round division down instead of ensuring a positive remainder?

The Euclidean division theorem, with which most math students and Haskellers are familiar, states that Given two integers a and b, with b ≠ 0, there exist unique integers q and r such that a = bq + r and 0 ≤ r < |b|. This gives the conventional…
dfeuer
  • 44,398
  • 3
  • 56
  • 155
11
votes
3 answers

Why int plus uint returns uint?

int plus unsigned int returns an unsigned int. Should it be so? Consider this code: #include #include #include class test { static const int si = 0; …
Vahagn
  • 4,190
  • 8
  • 33
  • 67
10
votes
2 answers

Why does Rust's u64.pow expect a u32?

Why is it that Rust's u64 primitive expects a u32 exponent? error[E0308]: mismatched types --> src/protagonists.rs:13:25 | 13 | return root.pow(self.secret) % prime; | ^^^^^^^^^^^ expected u32, found u64 help:…
joedborg
  • 15,097
  • 30
  • 72
  • 112
10
votes
4 answers

How to perform ceiling-division in integer arithmetic?

It's basically returning the boxes_needed. 1 box can contain 10 items. So if the items typed by the user is 102 then the code should return 11 boxes. Is there a way to divide that rounds upwards if there is a non-zero remainder?
Newbie_Android
  • 185
  • 2
  • 2
  • 11
10
votes
9 answers

Perfect square and perfect cube

Is there any predefined function in c++ to check whether the number is square of any number and same for the cube..
d3vdpro
  • 2,572
  • 4
  • 23
  • 28
8
votes
2 answers

How to implement arithmetic right shift in C

Many lossless algorithms in signal processing require an evaluation of the expression of the form ⌊ a / 2b ⌋, where a, b are signed (a possibly negative, b non-negative) integers and ⌊·⌋ is the floor function. This usually leads to the following…
DaBler
  • 2,471
  • 2
  • 24
  • 41
8
votes
1 answer

When to pick 'Natural' over 'Integer' in Haskell?

Not long ago I've discovered Natural data type in base. It's supposed to be taken (as it seems to me) when you intend to use non-negative integer type. But it's not exactly clear why should I prefer Natural to Integer. Both types have arbitrary…
Shersh
  • 8,297
  • 3
  • 27
  • 54
8
votes
2 answers

How to divide by 9 using just shifts/add/sub?

Last week I was in an interview and there was a test like this: Calculate N/9 (given that N is a positive integer), using only SHIFT LEFT, SHIFT RIGHT, ADD, SUBSTRACT instructions.
Tracy
  • 1,919
  • 2
  • 22
  • 33
8
votes
1 answer

Big number arithmetic using LLVM (from Haskell)

An answer to my previous question indicated that Haskell represents plusWord2# as llvm.uadd.with.overflow. I'm looking to do long addition with carry, like how the x86 ADC instruction works. This instruction not only adds it's two arguments, but…
Clinton
  • 20,364
  • 13
  • 59
  • 142
1
2
3
25 26