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.
396 questions
3
votes
2
answers
849
views
Three non-negative integer encoding techniques in Java
Intro
In this post, I will present three (3) non-negative integer encoding techniques:
Elias gamma coding
Elias delta coding
Golomb-Rice coding
Code
...
5
votes
1
answer
407
views
Positive Integer Class Supporting Arbitrary Number of Digits
I have implemented an elementary positive integer class that supports addition, subtraction, and multiplication for an arbitrary number of digits using a singly linked list.
...
5
votes
2
answers
849
views
Bruteforce integer multiplication in Java
Intro
This time, I have attempted to prove the following bruteforce multiplication formula:
$$
(a_n \cdots a_0) \cdot (b_m \cdots b_0) = \sum_{i = 0}^n \sum_{j = 0}^m a_i b_j \cdot 10^{i + j},
$$
...
13
votes
5
answers
3k
views
Infinite precision integer in C++20
This is my infinite precision integer implemented in C++20. It supports negative numbers. I have implemented addition, subtraction, multiplication and binary integer division, which returns quotient ...
-4
votes
1
answer
393
views
int128 handling in C code, gcc / glibc / linux - follow up IV [closed]
[edit] Hint: a similar project for C++20 which - as far as I see - also manages bigger than 128-bit integers can be found at: Infinite precision integer in C++20 . [/edit]
I got lots of kind hints on ...
3
votes
3
answers
621
views
int128 handling in c-code, gcc / glibc / linux - follow up III
I got helpful reviews for first steps in:
int128 handling in c-code, gcc / glibc / linux,
int128 handling in c-code, gcc / glibc / linux - follow up and
int128 handling in c-code, gcc / glibc / linux -...
3
votes
1
answer
254
views
Int128 for handling large numbers bassed off of BigInteger update
An update to Int128 That is built on Linux for lack of direct support, based on System.Numerics Nuget package.
...
3
votes
2
answers
909
views
int128 handling in c-code, gcc / glibc / linux - follow up II
I got very nice reviews for an attempt to produce read and print
routines for 128-bit integer datatypes in:
int128 handling in c-code, gcc / glibc / linux and:
int128 handling in c-code, gcc / glibc / ...
4
votes
1
answer
249
views
Int128 for handling large numbers based off of BigInteger
I have created Int128 which is based off of BigInteger with operator overloading to handle larger math requirements. Here is a ...
5
votes
2
answers
415
views
int128 handling in c-code, gcc / glibc / linux - follow up
I had a draft for int128 handling reviewed there:
int128 handling in c-code, gcc / glibc / linux
I changed a lot according to the hints there, while leaving in, e.g.,
a trailing space on each line, ...
3
votes
0
answers
130
views
Comparing two Tree sort algorithm variations implemented in Java
I have this repository. It contains three variations of a simple sorting algorithm for integer keys.
The idea is that we keep a balanced BST in which each node holds the integer key and its frequency. ...
2
votes
1
answer
263
views
Pollard's rho algorithm implementation
I used std::multiset to determine how many times that factor appears.
I'd like to know if it can be improved; I've focused on the algorithm rather than the code.
...
2
votes
1
answer
186
views
What is the most efficient way to figure out if a single number is prime for numbers from 2 up to 2,147,483,647 in Java?
As Java programmers, we can always use the BigInteger isProbablePrime() method or store manually all prime numbers in a HashMap. This question is about the most efficient way to figure out if a single ...
6
votes
2
answers
333
views
Bit manipulation to find a monochromatic clique on k vertices knowing all on k-1 vertices
I have a very hot segment of code in a large project. I've extracted the relevant segment and a toy example to illustrate it.
The project involves an unavoidable combinatorial explosion related to ...
3
votes
2
answers
338
views
im2double and im2uint8 Functions Implementation for Image in C++
This is a follow-up question for conv2 Template Function Implementation for Image in C++ and An Updated Multi-dimensional Image Data Structure with Variadic Template Functions in C++. For performing ...
5
votes
0
answers
115
views
Dial's heap in Java for integer priority queues
(The entire project is here.)
Intro
I have this priority queue data structure for non-negative integer priority keys. I recall that it is called Dial's heap.
Code
Implementation
...
9
votes
3
answers
1k
views
C function to read only numeric values
I'm learning C and as exercise I'm trying to implement a custom function to parse a input from user and return successfully only if the input is a number.
These are my files, how could I improve it?
...
6
votes
1
answer
320
views
Identify distance degeneracies on square and hexagonal lattices
I have an interest in how the number of ways you can reach from the origin to a certain distance on both square and hexagonal lattices. If L is the square of the distance and integer pairs (i, j) are ...
1
vote
1
answer
175
views
Calculate the cost of Biryani lessons
Problem:
According to a recent survey, Biryani is the most ordered food. Chef
wants to learn how to make world-class Biryani from a MasterChef. The
chef will be required to attend the MasterChef's ...
5
votes
1
answer
305
views
uint24_t implementation ported to Windows
I coded stuff for an embedded device where int is 24bits. I wanted to port uint24_t and ...
3
votes
1
answer
148
views
Any amount bits integer
This class is meant to work exactly as an unsigned integer should work, but it limits the value based on a set amount of bits.
...
6
votes
2
answers
247
views
C++ Arbitrary Precision Whole Number Class
The class below implements an arbitrarily sized whole number C++ class.
It is wrapped around a bitvector class, which does most the binary heavy lifting.
The basic ...
0
votes
1
answer
43
views
Mangle words to bytearray of lo/hibytes
I would like to refactor the following code snippet adapted from a project I'm involved. It is part of a code to control power outlets via network. The code snippet was written by myself based on the ...
2
votes
1
answer
200
views
Class for packing two 4 bit unsigned integer in uint8_t
The purpose for this class is to pack two 4 bit unsigned integer into 8 bit unsigned integer, e.g. uint8_t.
It is made in a way, so the class to resemble array of ...
6
votes
2
answers
213
views
First attempt at making a sobel edge detection filter in c
I made a borders filter in C. It works properly, but I think it could be optimized and better designed; the formatting is abysmal. It uses too many lines for the border cases and has too many ...
4
votes
2
answers
310
views
Print decimal integer
This is an x86-64 Linux program to print a non-negative decimal integer. I would appreciate any simple optimizations for size and readability. I am aware that division by a constant is usually done by ...
1
vote
3
answers
220
views
Simple stack of integers
I've a simple push/pop implementation in my program:
...
6
votes
2
answers
6k
views
128-bit integer type in GCC/Clang
When working on my projects, I often need a 128-bit integer type. For this purpose, I use the __int128 compiler extension. Here is the implementation I am currently ...
3
votes
1
answer
383
views
Generic Integer Square Root
I have implemented an integer square root function that is branch-free and runs in constant time, using the first variant found in this answer as a base. All possible values for the types ...
1
vote
2
answers
534
views
Mapping bunch of integers to some strings with less repetition [closed]
I have this switch case statement which maps an integer to a string. There can be multiple integers that maps to same string. objective here is to not to duplicate the string value and retrive the ...
8
votes
4
answers
3k
views
Standard C overflow-safe arithmetic functions
If this code is written correctly, it will block undefined behavior from happening in the case of signed overflow (guaranteeing that the program halts instead), and also guarantee that the program ...
3
votes
1
answer
529
views
Portable integer to/from little endian conversion in C
Integers need to be converted to a byte array of defined endianness to be reliably and consistently saved and transmitted, and converted back to be accurately received and read. The goal is to be as ...
4
votes
1
answer
711
views
Python function: user input to get positive integer using assert
Is it a bad thing to use asserts to validate user inputs? Or should I keep using an if statement in case the number isnt positive?
Code with assert statement:
...
2
votes
1
answer
128
views
Absolute value meta programming
I am implementing a generic absolute value function that handles signed and unsigned integer values correctly over the input type's domain. The std::abs(...) ...
6
votes
4
answers
2k
views
Find all ways to express each of a list of even numbers as the sum of 2 positive, even numbers
This code was a challenge from a friend. Essentially she wanted a way to
"Divide an even number into 2 even numbers."
So I made this quick and dirty Python program to find the solutions up ...
1
vote
1
answer
75
views
SortedIntSet: a set of integers that can be iterated in ascending order in O(N) time
I needed a set with the following properties:
can be iterated in ascending or descending order in \$O(N)\$ time
the usual set operations (contains, add, remove, discard) can still be done in \$O(1)\$
...
5
votes
2
answers
1k
views
Sum numbers digit by digit
The problem goes like this:
Given two one-dimensional arrays, for example a = (3, 4, 5) and b = (5, 6, 9), write a function ...
1
vote
4
answers
310
views
Spelling Java long values to human readable text in English
Now, I have extended my program taking some answers in the previous iteration into account:
...
5
votes
4
answers
2k
views
Converting int values in Java to human readable strings in English
(See also the next iteration.)
I have rolled this short program that converts int values to the human readable strings:
...
1
vote
1
answer
2k
views
Integer array to string
This function creates takes an int * buffer and creates a neatly formatted string (useful for printing the contents of an array).
Is the code easy to follow?
Is it ...
0
votes
1
answer
246
views
C++20 Wrapper class for an unsigned integer type and a templated derived class that codes modular arithmetic type
The context is a library to represent numbers (in this case integers) by wheighted position by radix powers for every radix R, passed as template argument. These derived types are digits of radix R, ...
3
votes
1
answer
78
views
Quickly read numeric input
Do you have any criticisms or corrections or improvements?
I need to read only numbers(and this numbers could be from 0 to 1000000)
...
2
votes
1
answer
250
views
Encode non-negative integer to a string of ascii letters and digits
I want to encode a non-negative integer to ASCII letters and digits.
I wrote below code for that, that works, but which I assume too immature to use in production.
...
1
vote
1
answer
90
views
C code for discovering numbers with interesting/beautiful/symmetric properties
Below is a C code that generates 1000 random positive integers, for each number we check primality, palindromic property, and the square root of the sum of the digits. The purpose is to find/discover ...
3
votes
1
answer
3k
views
Format an integer with space as thousand separator
I need to format a number with space as thousand separator in my Java code for Android.
How can these functions be improved?
What are the pros and cons to the first recursive method?
What are the ...
19
votes
4
answers
4k
views
Minimal `printf` for integer types in x86 assembly
I'm writing a minimal C runtime targeting an old 32-bit Windows XP machine as a personal project. The C runtime provided by compilers is quite bloated. I wouldn't mind some library bloats up to ...
4
votes
3
answers
1k
views
C++ money class for basic banking application
I wanted to get my code reviewed as I have tried to implement a basic Money class to represent money values in c++. Can anyone tell me if I am implementing my basic arithmetic operator overloads ...
6
votes
3
answers
2k
views
Count the numbers in a range in which the sum of the odds digits have the same parity of the evens one
As already written in the title I want to count the numbers in the range (x+1, y) where x and y are integers, in which the sum of the digits in odd places have the same parity of the ones in even ...
0
votes
3
answers
932
views
2
votes
3
answers
929
views
Convert string to integer and catch errors
I have attempted [and made one] function to convert string to integer and catch errors in pure c
Working method
The whole working method is present in code itself
The header file
The str2num.h here:-
<...