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.

learn more… | top users | synonyms (1)

8
votes
2answers
120 views

Simple function to verify if a number is integer

Sometimes, there's the need to verify if a given number is an integer or a decimal. Since JavaScript doesn't distinguish between both, I've made an extremelly basic function. ...
18
votes
4answers
10k views
5
votes
2answers
53 views

Add two numbers stored as a list in F#

I am solving the following problem: You are given two lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the ...
12
votes
3answers
744 views

Safe multiplication of two 64-bit signed integers

The function below implements safe multiplication of two 64-bit signed integers, preventing overflow from occurring: ...
5
votes
3answers
627 views

Function that adds two integer strings

I wrote this method that adds two integers in the form of strings together. This way I can add large integers without overflowing. How efficient is the code that I wrote? I am by no means a great ...
6
votes
2answers
77 views

Finding the Greatest Integer and Ties

I'm trying to find the greatest integer of four integers and its ties. I used the following code. Is there anyway to simplify this? ...
7
votes
3answers
7k views

Simpler method to detect int overflow

To detect int overflow/underflow in C, I use the below code. What might be simpler and portable code? (that is: fewer tests) Assume 2's complement and don't ...
2
votes
3answers
90 views

Method that returns an integer chosen by user

Can I make this: if (oneFour == 1 || oneFour == 2 || oneFour == 3 || oneFour == 4) shorter, especially if I'm gonna need a wider range of integers? Do I need to ...
4
votes
3answers
197 views

Counting words, letters, average word length, and letter frequency

I'm pretty sure my code is mostly correct. I think I'm having formatting errors more than anything. I keep receiving warnings about "double to float and int to float,possible loss of data. Here is ...
10
votes
2answers
342 views

Convert Persian digits to English numbers

I use the following code to convert ۱۲۳۴۵ to ‍12345, effectively transliterating the persian numbers into latin numbers: ...
4
votes
3answers
1k views

Decimal/binary/hex converter

I've finally finished my converter and have determined that it works, but I'm trying to make it cleaner and/or more practical (primarily with the switch ...
7
votes
1answer
91 views

IntModulus long addition and subtraction

One of the remarks in this answer to my question concerning IntModulus says What's more problematic is that you don't have matching argument symmetry for add ...
13
votes
2answers
238 views

Modular arithmetic

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

Bignum library with arbitrary bases

I just started development on a new bignum library that can operate on numbers of any arbitrary base (up to 232). I want to get a good idea of where I can improve before taking things any further. ...
6
votes
2answers
102 views

IntModulus reloaded

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 ...
5
votes
3answers
183 views

K&R 4-12 recursive converter int to string

The exercise is: Adapt the ideas of printd to write a recursive version of itoa; that is, convert an integer into a string by calling a recursive routine. I wonder if I have correctly ...
12
votes
2answers
182 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 ...
4
votes
2answers
65 views

Test if arithmetic operation will cause undefined behavior

I've written a command line calculator as an exercise to get ready for my upcoming first programming job. The whole program source is around 450 lines of code, which I think is too long for a single ...
7
votes
2answers
326 views

Writing data of a certain integer type to a buffer

I have function which writes data of a specified type to a buffer. Here is the part of this which writes Uint8, Uint16, ...
5
votes
1answer
86 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, ...
1
vote
0answers
34 views

Big Integers and parallel execution with OpenMP

This is part of an implementation of Rabin-Williams signatures as described by Bernstein in Section 6 of "RSA signatures and Rabin–Williams signatures: the state of the art" using Tweaked Roots. The ...
5
votes
2answers
5k views

Separate numbers with commas

How should I approach refactoring this code? ...
2
votes
2answers
148 views

Float string to integer string

I have a float in the String strFloat. I wanted to convert it to a rounded integer also stored in a ...
2
votes
0answers
92 views

A better way to multiply and add big numbers represented with strings

I got a very unusual problem of adding and multiplying very big numbers (≥1e+100). So I've written simple functions that would operate on string representations of numbers, both as an input and an ...
3
votes
2answers
101 views

Mystery sum with placeholder digits

I came across this coding problem. Back in primary school, maybe you were sometimes asked to solve a fill-in-the-blank sum - or "mystery sum" - in which certain digits are removed and you had ...
3
votes
1answer
44 views

Arbitary Precision Addition

I made a bigint class that can add unsigned unlimited precision integers by splitting number into chunks less than 9999, and if a part is bigger than 9999 I take mod 10000 and carry 1 to the next ...
3
votes
2answers
267 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 ...
3
votes
3answers
292 views
6
votes
1answer
72 views

Having fun with JNI: formatting a number

I attempted some (easy) coding with Java Native Interface. This is what I have: six_pack_Neatifier.h: (autogenerated by javah) ...
4
votes
1answer
185 views

Bills and coin denomination program

I am currently in an intro C++ programming class. We have an assignment where we have to convert a dollar and cents amount, say $192.89, to bills and coins using the least amount of both. Below is the ...
2
votes
1answer
72 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\$. ...
10
votes
3answers
133 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 ...
5
votes
3answers
761 views

Number to words problem

This is a very long solution I've been working on to a Number to Words problem. I've identified that there's a lot of repeating logic (e.g. 'if writing > 0', 'writing = integer /',). How would you ...
3
votes
2answers
82 views

Given bit strings, perform a bitwise addition

I'm looking for code-review, optimizations and best practices. This code is a Java adaptation of code on geeksforgeeks.org. Please verify if space complexity is \$O(n)\$ where n is number of bits, and ...
4
votes
3answers
337 views

Truncating integer using string manipulation

I have a class with a data member that needs to be rounded up to a 2 digit integer, irrespective of the number of the input digits. For example: ...
9
votes
1answer
427 views

Mostly portable 128 by 64 bit division

I wrote this out of curiosity. It is based on the theory behind Knuth's Algorithm D and is intended to emulate the behavior of the x86 div instruction (though the ...
1
vote
1answer
316 views

Conversion functions between binary, hexadecimal and ASCII

I've written a small suite to easily convert my data between binary, hexadecimal and ASCII: ...
5
votes
3answers
278 views

Performing several integer division operations on an array

Program: Given an array A of size N. Given Q operations, each operation contains an integer D. In each operation you have to divide all the elements of the array by D. For example, ...
5
votes
3answers
238 views

atoi implementation in C

I'm looking for feedback on my atoi implementation here in C. Any feedback would great. I made my own atoi for fun and to learn ...
7
votes
2answers
698 views

Implementation of itoa()

How can this solution be improved? ...
5
votes
2answers
61 views

Distinguishing user input numbers and characters

I am currently taking a computer science class in high school, and it is my first time programming (2 weeks since I started). I have currently finished my program; however, there were a few details ...
8
votes
3answers
270 views

Keeping integer addition within bounds

I have to read a int8_t from a buffer, read a uint8_t from the camera's current settings, add them together, keep new value ...
3
votes
2answers
54 views

Program to evaluate powers of complex numbers

I'm trying to develop a simple program to evaluate integral powers of a complex number \$z\$ , that is, \$z^n\$, where \$z\$ is in the algebrical form \$a+i b\$ and \$n \in \mathbb{Z} ^{*} _{+}\$. ...
4
votes
1answer
118 views

Pollard p - 1 implementation

I'm trying to implement the Pollard p - 1 algorithm seen here, in Java. It has a C implementation here. I am using it for numbers without any smaller factors so trial factorisation isn't needed. My ...
3
votes
1answer
97 views

Network byte order to host byte order

While doing a little project involving sniffing packets, I found myself programming a cross platform network to host byte order and host to network byte order, this is my first time developing a ...
5
votes
2answers
103 views

Integers from scratch in Haskell

Inspired by this article about natural numbers from first principles in swift I implemented integers from scratch in Haskell. Besides obviously being extremely inefficient, is this code idiomatic ...
3
votes
1answer
57 views

Parsing a long without using inbuilt function parseLong

I came across it as a problem to solve. What kind of limitations are there? ...
3
votes
1answer
53 views

Fast power in Go

I just started learning Go. To start playing with it, I've implemented the fast power algorithm: Any suggestions or criticisms regarding the coding style? ...
4
votes
3answers
260 views

Partition a list of numbers into even and uneven numbers

Input: An array of integers. Output: That same array, but with the even numbers at the front and the uneven numbers at the back of the array. I wrote three different solutions, depending on the ...
1
vote
1answer
33 views

Counting how many LCD segments are needed to display a number [closed]

I made a program which reads digits of series of numbers and then tells you which would need the largest number of "lines" to be written with a classic digital calculator (example: for 1 you need two ...