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.

Filter by
Sorted by
Tagged with
2
votes
2answers
4k views

Python combine two integers to a third

We have three integers: A, B, C where the first (i.e. the most significant) digit of C is the first digit of A, the second digit of C is the first digit of B, the third digit of C is the second ...
3
votes
1answer
60 views

Turning an Integer into an array of ints

My goal here is pretty simple, I want to turn a number like 12435987234 into an array of integers in reverse order. So that would look like: ...
4
votes
3answers
316 views

Convert a number into a different base and return as string

I wanted to write a function that would take a long long int argument as well as a base and it would convert that number into an equivalent number in a different ...
4
votes
4answers
10k views

Integer-to-hex string generator

I am using this as a learning exercise primarily and want to ensure my code is optimal. The main thing is that it is reliable and I am made aware of any flaws. Could it be made more efficient or ...
11
votes
4answers
948 views

Detecting arithmetic overflow in C with NASM

This snippet is about detecting whether the carry flag is set or cleared. I work on Mac OSX, so my snippet supports Mac only. First, we need a routine that does the job: func.s: ...
8
votes
2answers
316 views

Implementing min of 3 numbers in Mic-1 code

I have the following algorithm to find a minimum of three input numbers: ...
6
votes
1answer
545 views

C++ console program

I have been learning C++ programming. What you see here is all that I know how to do at this point. I look forward to learning more and I know there are better ways to accomplish a task such as ...
10
votes
1answer
5k views

Converting a range of integers from a string to an IEnumerable

Goal Convert the following into an IEnumerable of integers accounting for the x-y ranges: ...
7
votes
1answer
6k views

Combining two 32-bit integers into one 64-bit integer

long long combine(unsigned int high, unsigned int low) { return ((unsigned long long) high) << 32 || low; } Is there a better name for the operation? Is ...
8
votes
2answers
190 views

All arithmetic operator functions (+, -, *, /, %) coded only using bitwise operators in C

Here's all the functions in C that does all the arithmetic operators without using arithmetic operators themselves, mostly using bitwise operators. Recursive functions and comparison operators are ...
1
vote
2answers
5k views

Number to german decimal format converter function [closed]

I have a function that formats a number (even a float) to a German decimal format. I got this function from here https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-...
2
votes
3answers
224 views

Long arithmetic addition in JS

The implementation of the algorithm which adds two number in string form with arithmetic rules. ...
5
votes
1answer
881 views

Floor and Ceiling to specified int

This relates to Round to specified int but I'm separating these methods out since they're technically different. Is there a method that already performs a custom floor and ceiling to a specified ...
5
votes
1answer
4k 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? ...
11
votes
5answers
5k views

Maximum product of 3 integers in an int array

I want to find the maximum product that can be obtained from any 3 integers in an integer array. The optimal solution has time complexity of \$O(n)\$ and space complexity of \$O(1)\$. I managed to ...
3
votes
5answers
2k views

Two integers x & y

Given two int values, print whichever value is larger. However if the two values have the same remainder when divided by 5, then the print the smaller value. However, in all cases, if the two values ...
2
votes
1answer
129 views

Text representation for numbers with python

Simple function which returns a text representation for numbers in range [1...1000]. ...
11
votes
7answers
7k views

Numbers to byte-arrays and back

As the title explains, this is a series of extension methods that convert certain numeric types to and from byte-arrays, for certain actions which work better on byte-array types than numeric types. ...
0
votes
2answers
83 views

Program to calculate smallest number with permutations divisible by a set of numbers

(EDIT: here is the follow up question) I have written a small program in C to calculate the smallest number where any of its permutations or the number itself is divisible by a set of given numbers. ...
7
votes
4answers
289 views

Add one to a very large integer

I was asked to implement this in an interview a while back. I didn't pass the interview, mostly likely because it took me far too long to do. However, I'm interested to know how well implemented the ...
3
votes
1answer
3k views

Little/Big Endian conversion

I wanted to make sure that my code is properly converting between the two endians. Here is the code where I read in an integer: ...
6
votes
0answers
625 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. ...
8
votes
4answers
6k views
6
votes
2answers
2k views

Reverse binary representation of an int (only significant bits)

This based on this question in StackOverflow. The accepted answer uses Convert.ToString(int, base) to get the binary string, ...
3
votes
2answers
1k views

C - K&R getint() variation

I'm currently learning C with "The C Programming Language" by K&R. At the moment I'm at the chapter about pointers (since I come from Java, they are new to me). In the book is an example code for ...
12
votes
1answer
1k views

Tuple<int, int> replacement

I use the following structure in a web service where performance is critical. It is used for making joins between domain data objects, the key is made of two integers. The idea is to fit the two 32 ...
8
votes
2answers
6k 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. ...
26
votes
4answers
48k views
5
votes
2answers
558 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 two ...
12
votes
3answers
5k 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
2k 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
91 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? ...
2
votes
3answers
98 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 ...
15
votes
2answers
11k views

Convert Persian digits to English numbers

I use the following code to convert ۱۲۳۴۵ to ‍12345, effectively transliterating the persian numbers into latin numbers: ...
7
votes
1answer
864 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 and ...
14
votes
2answers
742 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
277 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
122 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
373 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 answered ...
12
votes
2answers
204 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
165 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
414 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, ...
4
votes
1answer
646 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, ...
2
votes
2answers
184 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 ...
3
votes
2answers
319 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
72 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 part....
3
votes
3answers
367 views

Mockup in C of hardware implementation for multiplication of signed integers

I challenged myself to implement multiplication of signed integers using only +, -, >>,...
6
votes
1answer
523 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
7k 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
445 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\$. ...