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.
7
votes
3answers
251 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
38 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} ^{*} _{+}\$.
...
5
votes
2answers
44 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 ...
4
votes
1answer
31 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
39 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
79 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
35 views
Parsing a long without using inbuilt function parseLong
I came across it as a problem to solve. What kind of limitations are there?
...
4
votes
3answers
204 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
23 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 ...
2
votes
1answer
37 views
All-in-one number base converter
I coded this program as a challenge for /r/dailyprogrammer. Basically this program can convert any number from any base to another base including negative bases as long as the input is coherent. Did I ...
3
votes
1answer
29 views
Digit Summing Function
I'm working through the common divisibility tests, and implementing them in Racket. I'm working on divisible by three, for which I thought it'd be nice to keep summing until I got to a single digit ...
-3
votes
1answer
49 views
Simple betting game in python not working [closed]
So the problem I am having with this code is that when I run the code, there are no issues, however when it gets to the result of the dice roll, it does not subtract money if the person lost the bet, ...
4
votes
4answers
615 views
Squaring an integer by repetitive addition
I've written two functions:
is_integer(), which tries to get a positive or a negative integer as input and returns the same value.
...
2
votes
2answers
72 views
Writing an API for checked subtraction
I have to write an API for checked subtraction where the input minuend can be < 0 but subtrahend can't be < 0. I think I've covered all the cases of underflow and overflow. With given conditions ...
6
votes
4answers
2k 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 ...
3
votes
1answer
48 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?
...
3
votes
2answers
64 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 ...
10
votes
1answer
252 views
Multiplication 128 x 64 bits
I was fooling around with the Collatz sequence a bit and found out that long is only sufficient for starting values below ...
3
votes
3answers
70 views
Listing the representations of integer M as a sum of three squares M = x^2 + y^2 + z^2
I am trying a number as the sum of 3 squares. For my particular project I will require a complete list since the next step is to compute nearest neighbors (here).
In any case we'd like to find \$ M ...
6
votes
4answers
521 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 ...
7
votes
0answers
104 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 ...
12
votes
3answers
977 views
Random permutation of int[] and ArrayList<Integer>
I wrote a simple program using int[] and ArrayList<Integer> which aims to get a random permutation output between 1 to 10, ...
3
votes
4answers
209 views
Function to extract bytes as a decimal
You give this function a pointer and how many bytes you want to extract as a decimal and it returns it.
...
10
votes
4answers
313 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 ...
10
votes
1answer
149 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 ...
8
votes
4answers
796 views
Computing factorials
I am computing factorials as part of teaching myself C++. I am looking for feedback on efficiency, style and any bad practices I may be using.
...
5
votes
2answers
70 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 more effectively?
...
8
votes
1answer
164 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 ...
10
votes
3answers
579 views
Beginnings of a base-conversion game in Python
I'm pretty very new to python, so I thought that I'd write a game to teach me python and better teach me how to think in hexadecimal and binary. I'm starting off by writing a few functions to convert ...
8
votes
2answers
210 views
Parsing integers safely
Background
I'm writing user input validation for a client-side JavaScript web application. Users enter numerals representing an integer into an <input>. As ...
8
votes
2answers
134 views
Lossy packing 32 bit integer to 16 bit
I am working on the lossy 16 bit representation for 32 bit integers that catches all 32 bit range with precision depending on absolute value.
My idea is to store integer ...
5
votes
4answers
186 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 ...
2
votes
1answer
190 views
Encoding and decoding exercise
I was issued the following coding challenge from a company to write an encoding and decoding function:
Encoding: This function needs to accept a signed integer in the 14-bit
range [-8192..+8191] ...
3
votes
2answers
151 views
Emulating __uint128_t
I developed a lot of code locally with __uint128_t only to find out that it is not available on the target platform.
I am now trying to write my own struct that ...
0
votes
1answer
106 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. ...
10
votes
3answers
2k views
More elegant way to increment Integers in Map?
I have a HashMap<Token, Integer>, which counts occurrences of Token. Each time Token ...
5
votes
2answers
403 views
Why are these functions slower than BigInteger's included methods?
I tried writing alternative functions to Java's BigInteger.add(BigInteger.ONE), BigInteger.add(BigInteger), and ...
2
votes
1answer
267 views
Tough List Comprehension Split Letters and Numbers
So, another challenge from my professor. I've finished it, but wanted to get some feedback, and see how other people might work this problem.
Assume you have a string, such as "Hello 34215 World ...
10
votes
1answer
802 views
Thread-Safe Integer Sequence
I would like to give an Integer to whatever demands it. For that I use the following code
...
3
votes
2answers
273 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?
...
5
votes
1answer
46 views
Converting the integers to their equivalent string representations
I have the following function which converts an integer (such as 103) to its string representation ("one hundred three"):
...
2
votes
2answers
382 views
Two solutions with min max values - which should be slower?
While practicing at codinbat.com on the assignment:
Given an array length 1 or more of ints, return the difference between the largest and smallest values in ...
2
votes
1answer
353 views
Write int, short int or any integer value in a file in binary form using fputc
I am trying to write binary values in a file using fputc. In my case it is a short int.
The file must be opened in "w" mode.
...
4
votes
3answers
8k views
Converting an integer to binary, hex, and octal
I've started caring about standards and the beauty of my code. I doubt whether I've written it correctly and aesthetically. Can you please judge this simple, exemplary program? I want to know of any ...
3
votes
3answers
4k views
3
votes
2answers
58 views
Rounding ints division result to certain “granularity”
In my program I call "granularity" the "minimum step" for a number. For example, if granularity == 4 then I only work with numbers "0, 4, 8, 12 etc" (so normal ...
11
votes
2answers
1k views
Division without / operator
I created a method that does division without the / operator. I believe this code is a bit slow and can be improved, but I am not sure how to do so. I'm not so good ...
4
votes
2answers
216 views
fractions data type — when to reduce?
Here is my Python implementation of a simple fractions class:
...
8
votes
2answers
135 views
Container for list of ranges
I made a C++ container of an ordered list of integers, that, in order to save space, I save as ranges:
For example, the list \$ \{1,2,3,5,6,7,8,9,20\} \$ is saved in memory as \$ \{(1,3), (5,9), ...
6
votes
3answers
307 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 ...