Questions tagged [integer]

Use this tag for questions about using, storing or manipulating integral values of all types and sizes, including concerns about overflow. Not for code that casually happens to use integers.

45 questions
Filter by
Sorted by
Tagged with
3k views

Computing the integer square root of large numbers

The "integer square root" of a non-negative integer $n$ is defined as the largest integer not greater than $\sqrt{n}$:  \operatorname{isqrt}(n) = \lfloor \sqrt{n} \rfloor = \max \{ k \in \...
4k views

Computing the square root of a 64-bit integer

When sieving primes up to some 64-bit number n I need to determine an upper bound for the greatest potential factor of n, which ...
361 views

Improved Van Emde Boas tree based map in Java

After working on Van Emde Boas tree -based map in Java, I came up with this code. I managed to fix the bugs and improve the performance by using internally primitive integers instead of wrapped ...
2k views

Optimizing unique partitions of integers

I am working on a project involving the unique partitioning of integers. I.e. I am looking to have all of the partitions of n = x1 + x2 + x3 + ... + xk, where xi = xj implies i = j. For example, if I ...
122 views

As my question Modular arithmetic was hard to review and terribly long, I've added a few comments to the easier part, namely IntModulus. The code uses Guava and ...
271 views

BigRational based on BigIntegers

For Project Euler I've implemented a BigRational. The functionality is rather limited, just things I needed or will probably need soon. The style departs a bit from ...
3k views

Huge integer class using base 256

After a hiatus, I have returned to coding in C++. In an effort to learn some of the newer aspects of the language and for exercise and 'up-skilling', I am writing some simple classes and this ...
9k views

Implementation of C Standard Library Function ntohl()

This is an implementation of ntohl() that I wrote as an exercise. ntohl() takes a uint32_t ...
4k views

Integer-to-ASCII algorithm (x86 assembly)

This is my best effort at converting a 32 bit integer in EAX, to an 8 character ascii string (result in RDI). It will work ...
1k views

Sum of digits in a^b

I'm trying to calculate a problem which requires sum of digits in $a^b$, where $0 \le a \le 9$ and $0 \le b \le 4000$. I have seen various other posts similar to this, but I haven't acquired ...
3k views

Successful use of strtol() in C

Attempted to use strtol() to parse a string into an integer and to output any appropriate error messages for failed conversions. How did I do? How could/should it ...
732 views

'StrictInt' Python object class that prohibits casting numbers with non-integer components to int

Inspired by and created because of python3 utility: convert safely to int and driven partly by the 'refactor' in my answer. The aforementioned question and my answer drove me to go and do a thing ...
140 views

Arbitrary large unsigned integers

Edit: There's now a Follow-up question concerning more recent code. From time to time, you see a question over at Stack Overflow about calculating factorials, typically asking why the result soon ...
2k views

Custom integer class for calculating crypto algorithms

How can I improve upon this integer class that I wrote? I am/will be using this for some calculation intensive crypto algorithms, mainly POLY1305AES. I might even write RSA using this. I know that ...
218 views

Custom integer parser optimized for performance

I want an integer parser so fast it risks discovering time travel. I'm stuck in C# for a language, but that doesn't mean I can't try to use c++. So I wrote the following: The best I could do ...
19k views

Split a long integer into eight 4-bit values

It's an exercise from a book. There is a given long integer that I should convert into 8 4-bit values. The exercise is not very clear for me, but I think I understood the task correctly. I googled ...
5k views

Round to specified int

This accepts values from a user that needs to show up in a intervals of some number. In this case, we will say intervals of 5. Is there a better way to do this? ...
1k views

HackerEarth Girlfriend's Demand challenge, solved two ways

I solved the Girlfriend's Demand challenge on HackerEarth. In summary: Input: First line of the input contains a string S. Next line contains an integer Q, the number of queries. Each of the next ...
303 views

Computational verification of Collatz conjecture

Prerequisites The typedef name uint128_t designates an unsigned integer type with width exactly 128 bits. The ...
847 views

Maximum product of 3 integers in an int array using Python

This is an interview question from Interview Cake: Given a list_of_ints, find the highest_product you can get from three of the integers. The input list_of_ints will always have at least three ...
784 views

Modular arithmetic

As the name says, LongModulus is a pretty long (lengthy) class implementing modular arithmetic for a long modulus. As overflow ...
1k views

Arabic to Greek numeral converter, 1 - 10

One of the things that's straight-forward in JavaScript is Unicode support. If you want to return ϝ (lowercase digamma, not a simple ...
206 views

UIntArray class in PHP (part 2)

This question is the follow up of the question: UIntArray class in PHP @Brythan provided a really nice review and I have improved my class. I have addressed almost all the issues he pointed out (...
185 views

UIntArray class in PHP

I have seen UInts being natively supported in other languages. PHP allows you to install a third party extension, but I can't do that ATM, so I've decided to create ...
2k views

C++17 saturating integer (arithmetic) type library

I wrote a C++ library to provide extra signed and unsigned integer types that saturate in overflow situations. It's in proof of concept stage and I'd love to get some feedback on it. A short usage ...
351 views

Fastest sum of absolute values of 32 differences

A long should be treated as a list of 32 unsigned numbers, each of them just two bit long. So 0x1234F... represents ...
402 views

Implementing min of 3 numbers in Mic-1 code

I have the following algorithm to find a minimum of three input numbers: ...
690 views

Recursive factorial calculations in Rust

I am trying to calculate very large factorials. I am looking to speed it up as 10000! takes approximately a minute and a half. ...
3k views

Swapping two integer numbers with no temporary variable

I tried to swap 2 integer numbers without using an additional variable as a traditional swap. Is it legal in C++? My VC compiler doesn't complain nor gives any warning about it. If so, how can I ...
3k views

Hamming distance between numbers in JavaScript

In Leetcode it states that my runtime is only faster than 38% all of submitted JavaScript solutions. Is there anything I can change to make it more efficient? ...
110 views

Enforcing correct input/output of integers

The problem: Some types, like cstdint aliases or, at least purely theoretically, even std::size_t, may be (but don’t have to be)...
389 views

How to optimize bitwise get/set/clear of ranges of bits on 8-bit integers in JavaScript?

I have been working for a few days on writing get, set, and clear bitwise functions in ...
99 views

BigInteger check in C from a string

I have started to learn C. At the time of writing I don't know anything about the language. The following I wrote today in a few hours. Though I do have a lot of programming experience, I find C hard ...
275 views

C library that converts integers to string and vice-versa

I've created this little library to help ease myself (and for others, hopefully) with the pain of having to convert integers to string and vice-versa. It's written in C, and for maximum portability I ...
76 views

F'up: Arbitrary large unsigned integers

This is a followup question to Arbitrary large unsigned integers. The feedback was very helpful and made me redesign large portions of the code. I changed the ...
709 views

Saturated arithmetic

For project Euler, I need quite often to test things like is x*y*z < 1e18. Since BigInteger is usually too slow, ...
219 views

Reverse int within the 32-bit signed integer range

Problem Reverse digits of a 32-bit signed integer. When the reversed integer overflows return 0. Optimized code here. Feedback I'm looking for any ways I can optimize this with modern C++ ...
917 views

Reverse int within the 32-bit signed integer range: $[−2^{31}, 2^{31} − 1]$ Optimized

LeetCode Problem Reverse digits of a 32-bit signed integer. When the reversed integer overflows return 0. Feedback Optimized from beta code in the original question here. Based on this LeetCode ...
489 views

Function for splitting an integer into smaller values

This is the function I've implemented in an answer to this question. I've tried to make this as simple and idiomatic as possible, using C++11. Could this still be improved in any way? ...
1k views

Handling GetTickCount() overflow in timeouts

This is code which must run on XP, so no GetTickCount64, and which should correctly handle the value wrapping around after 49 days. Can it be improved? ...
228 views

The implementation of the algorithm which adds two number in string form with arithmetic rules. ...
528 views

SPOJ problem - The last digit of a number to a power

Here is the problem statement: Given integers $a$ ($0 \le a \le 20$) and $b$ ($0 \le b \le 2,147,483,000$), where $a$ and $b$ are not both 0, find the last digit of $a^b$. ...
128 views

Link to original question: Successful use of strtol() in C After getting excellent feedback from the code review community I attempted to redo my string to long conversion program! I believe I ...