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
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 ...
6
votes
3answers
18k 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 ...
15
votes
2answers
10k 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
851 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 ...
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 ...
14
votes
2answers
716 views

Modular arithmetic

As the name says, LongModulus is a pretty long (lengthy) class implementing modular arithmetic for a long modulus. As overflow ...
5
votes
3answers
366 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 ...
4
votes
2answers
157 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 ...
10
votes
1answer
6k views

Multiplying and adding 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 ...
4
votes
1answer
611 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, ...
3
votes
2answers
309 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
70 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....
5
votes
2answers
1k 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 next ...
3
votes
3answers
366 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
495 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) ...
2
votes
1answer
431 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\$. ...
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 ...
10
votes
3answers
343 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 ...
3
votes
2answers
143 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
455 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: ...
3
votes
1answer
17k 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
3k 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, for ...
7
votes
4answers
706 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 ...
8
votes
3answers
319 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
844 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
513 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
1k 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
1k 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
343 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
328 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
678 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 ...
4
votes
1answer
7k 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
359 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 (...
5
votes
4answers
3k 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
92 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 ...
5
votes
5answers
3k 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
77 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
253 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 ...
15
votes
1answer
2k 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 ...
4
votes
3answers
454 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
1k 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 ...
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 (...
12
votes
3answers
6k 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
1k 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. ...
11
votes
4answers
4k 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 ...
11
votes
1answer
184 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
3k 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. ...
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? ...
10
votes
2answers
3k 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
760 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 ...