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
3
votes
0answers
526 views

Miller-Rabin Primality Test Using C++ and GMP

I'm currently writing a Miller-Rabin primality test using C++ and the GNU MP Bignum Library (GMP). As I've only started learning C++ about a day ago, I'm sure this code is riddled with errors and ...
4
votes
3answers
177 views

Caching an integer - distinguishing no value from zero

I would like to store an integer value in the application cache. The GetOrSet method should always return an int, and never be null. If there is no cached value, ...
4
votes
0answers
60 views

C++ library with tools for (arithmetic) type manipulation and mixing

Broken out from a previous question (C++17 saturating integer (arithmetic) type library) is a small C++17 header-only library to deal with issues that arise working with arithmetic where signed, ...
1
vote
2answers
2k views

Reversing the bits of a 32-bit integer

Here is my code: ...
5
votes
1answer
83 views

Printing limits of Integral Types in C

I intend to demonstrate the proper way to use format specifiers for different data types and also display the respective limits. This is meant to be an example. ...
3
votes
2answers
241 views

Reading lines of integers into a list of lists

I have a text file containing integers in different lines which I want to read into a list of lists. So 01 11 14 45 50 09 should become ...
15
votes
2answers
4k views

Big integer class in C++

I have been trying my hand a bit at creating a big integer class in C++. This is a continuation of a homework assignment. The class stores a big integer in a double linked list. Each slot contains 8 ...
2
votes
1answer
5k views

MIPS assembly string to int function

I have written the following code for a programming assignment. I would appreciate some help to improve this code and see if it meets all the requirements and criteria. I would like to improve my loop ...
6
votes
2answers
470 views

Variable integer encoding

There is a varint encoding, which I call LEP64 (Little Endian Prefix Max 64-bit). Basically, the first byte of the encoded value tells us, how many bytes follow (needed bytes encoded in unary coding, ...
10
votes
2answers
1k 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 ...
4
votes
3answers
731 views

Simple Java program - Coding bat sumNumbers

The problem is stated as follows: Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row. (Note:...
4
votes
1answer
168 views

Non-overflowing avg calculation with integer arithmetic

I have multiple long values and I want to calculate their average value using integer arithmetic, without precision loss and using java rounding rules for division, ...
9
votes
4answers
6k views

Format an integer with thousands separator (recursive implementation)

The function accepts a number n and displays it recursively with commas. As well as a general code review, I'm interested in suggestions for alternatives to ...
11
votes
2answers
2k 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 \...
4
votes
3answers
366 views

Parse a stream of space separated numbers

I need to parse a stream of characters IEnumerable<char> as a list of space (and newline) separated numbers. Numbers can be expressed in base 10, base 8 (...
0
votes
3answers
175 views

Divisions using Arraylists

Exercise: Using Arraylists create a program that makes divisions between two very large numbers (numbers greater than 10 digits). I did it (the code works), but could my code be improved? ...
1
vote
2answers
685 views

String of numbers to Integers (without using .to_i)

My exercise was to take a string of numbers, such as "1234" and return them as an integer 1234. However, I wasn't allowed to use #to_i, ...
5
votes
3answers
677 views

BigInteger class, supporting addition only

Here is just addition, no multiplication or subtraction, but I will implement them later. Before going on, I'm here to ask for advice about the design. ...
7
votes
5answers
447 views

Sum a list of string numbers

I have a list of 32-bit signed numbers in range [-10e5, 10e5] in string form read from stdin, and I would like to compute and print their sum in the fastest way possible. The bottleneck is in the ...
3
votes
1answer
2k views

Validation of integer inputs from a BufferedReader

At my school, we are using the BufferedReader for keyboard input for our console apps and I don't want to have to write ways to validate input for every project so I am writing a class to validate ...
7
votes
2answers
410 views

A program which adds 2 integers in the range of 0 to 10^100

I wrote this program to help myself learn C. It is the result of a few days of hacking things together until it worked. This program takes 2 integers as inputs in the range of 0 to 10100 and outputs ...
10
votes
5answers
16k views

Checking if a number is power of 2 or not

This is my solution in Java and I need your opinion: ...
6
votes
2answers
393 views

Shifting an 128-bit integer consisting of four 32-bit integers

For a little project of mine I have written two versions for shifting a 128-bit unsigned integer consisting of four 32-bit unsigned integers in x86 assembly. I cannot really decide which is better in ...
2
votes
1answer
725 views

Beautiful Number Validator

Problem Adapted from this HackerRank problem. A numeric string, \$s\$ is beautiful if it can be split into a sequence of two or more positive integers, \$a_1, a_2,...,a_n\$, satisfying the ...
2
votes
1answer
319 views

WGS84-coordinate encoded as Integers

I want to store WGS84-coordinates in two integers for latitude and longitude instead of floats to get a higher precision than with floats. Here's my code: ...
5
votes
1answer
2k views

Simple BigInt for sum and difference

I am working on a school project where we need to add and subtract large integers in C++. I will also be adding multiplication and division in the next couple days. So far it appears like everything ...
1
vote
0answers
61 views

Smallest difference of pair in String-array

Let's say I have the following array of Strings: String[] rows = new String[]{ "", "A", "B C C", "", "B D E", "E F G H" }; As well as the following ...
3
votes
1answer
3k views

MIPS assembly program to read integer byte by byte

I try to learn assembly by writing small programs. This MIPS program is supposed to read an integer from the terminal and print it byte by byte in hexadecimal. I think it works but I didn't check ...
4
votes
1answer
157 views

Abelian sandpile

I am trying to programm the Abelian sandpile model in VisualBasic. Also, here is a youtube video made by Numberphile about the very same topic. Here is a picture (\$2^{20}\$ grains of sand) I made ...
5
votes
2answers
345 views

Formatting big integers as hexadecimal digits separated by colons

I'm pretty sure this can be written as a couple of lines, but I'm unable to come up with a better solution. ...
3
votes
2answers
99 views

Efficient integer input / output (with a use case)

The task: A set of functions used to read/write integers from/to stdin/stdout as efficiently as possible. Rationale: printf/...
4
votes
2answers
69 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 ...
8
votes
2answers
133 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 ...
11
votes
1answer
2k views

Simple uint128_t implementation

I made a simple uint128_t implementation for a project that I'm working on. The reason for not using for example boost::uint128_t...
3
votes
2answers
288 views

Solution to itoa

As some of you might know I've been working my way through the C Programming Language (2nd). I recently finished a rough implementation of the itoa exercise (...
18
votes
7answers
14k views

Checking if each char in a string is a decimal digit

I had to write a program that would verify that a string contains any digits beside numbers 0-9 and the first '-' sign to indicate if pos/neg. The algorithm works like I want it to, but is there a ...
7
votes
1answer
282 views

Convert size_t to big endian on Linux

I see that on Linux there are various built-in functions to convert endianness for specific integer width types, and they are probably faster than anything one can write and compile on their own. But ...
4
votes
3answers
107 views

Count number of bits to convert one integer to another integer

Return the number of bits (N) that will need to be changed in order to convert an integer(X), into another integer(Y). Constraint: 0 <= X <= Y <= 2**128 Example: X = 1, Y = 2 => ...
6
votes
3answers
2k views

User input validation that might be unbreakable for C++

I am new to programming and have been having a lot of trouble with user input validation. Everything I found online I could break and I finally put this function together that appears to be ...
3
votes
2answers
448 views

Assembly: Sum up the single bytes of a 32bit-register to a checksum

Exercise description: "Write a program that takes a double word (4 bytes) as an argument, and then adds all the 4 bytes. It returns the sum as output. Note that all the bytes are considered to be of ...
6
votes
0answers
124 views

Find an arithmetic expression near to target value

I wrote this program as an answer to Express a Number challenge on the Programming Puzzles & Code Golf site; it may be worth looking there for the context. The concept is simple (and familiar to ...
3
votes
1answer
115 views

Counting digits of numbers

Write a program that loops prompting for positive or zero integers of data type long. Then the number of digits the integer consists of (in decimal representation) should be printed to stdout. ...
1
vote
1answer
1k views

Forced and easy hton/ntoh conversion

Context: When sending message across the network, the sender has to convert the message from host byte order to network byte order and the receiver has to do the the reverse - network order to host ...
1
vote
0answers
134 views

Wrapper for GMP in C++

I'm writing a C++ high precision library based on GMP, sample code(files which have to be added to project): ...
3
votes
1answer
4k views

Assembly 8086 program to input a 16-bit signed number (in string form) and output its binary equivalent

I'm working on an exercise using Assembly 8086 which inputs a number (in string form) then output the binary form. Here is what I did (tested on emu8086 by the way): Convert the string to numeric ...
5
votes
2answers
260 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 ...
2
votes
3answers
2k views

Assembly x8086 (emu8086) - Display 32bits number on screen

this is my code (assembly x8086, not MIPS, and I'm using emu8086) to display a 32-bits number on screen. Of course the basic algorithm is as follows: ...
5
votes
2answers
4k views

Adding two Integers in an Array

I was wondering if I could get a second look at my program before I turn it in. The job was to code a program in java that adds two integers to an array gather the sum while displaying the integers ...
5
votes
2answers
2k views

Implementing String to int (atoi) in Java

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus ...
3
votes
1answer
657 views

Integer to binary using an old-school way

I know I could achieve this using System.out.println(Integer.toBinaryString(num)); in Java. However, I wanted to do it this way. ...