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
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? ...
4
votes
1answer
342 views

Computational verification of Collatz conjecture using OpenCL

This code review request follows my previous request Computational verification of Collatz conjecture. Unlike the previous program (which was designed for the CPU), this code should run on modern GPUs....
5
votes
4answers
1k 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:...
1
vote
2answers
260 views

Function that adds commas between groups of 3 digits in a string

My program contains some very large integers and uses the GMP library. The output contains integers (probably small enough to fit into int). I would like to make ...
3
votes
0answers
640 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 ...
1
vote
0answers
148 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): ...
4
votes
1answer
78 views

Manually formatting numbers in Python

I was working on a little Python challenge that was a bit more difficult than expected: format any non-negative number so 1000 turns into ...
9
votes
2answers
399 views

Converting integers into Unary Notation

I have written a python script that has tallies representing digits 1-9 (0 is just ":" ) Unlike true Unary languages that are in the form 1^k. I represent values for n as per their decimal digits. The ...
0
votes
2answers
83 views

Python code to continue if a certain type is seen, otherwise try again to get valid input [closed]

I am very very new to programming, and this is my first day using Python. I want to check to see if an input is of type 'int' and to proceed with my program if the user has entered an 'int', ...
3
votes
2answers
80 views

Computational verification of Collatz conjecture using gmplib

Prerequisites The unsigned long type has a size of 64 bits. The following macros are defined. ...
3
votes
1answer
288 views

Computational verification of Collatz conjecture

Prerequisites The typedef name uint128_t designates an unsigned integer type with width exactly 128 bits. The ...
12
votes
3answers
1k views

Binary Converting Code

I made a program that converts a decimal number given from the user, and converts it to a binary number. For example, if I entered 2, it would return 10. It then, counts the amount of ones and zeros ...
2
votes
1answer
25 views

Invert bits of a number in clojurescript

Doing a challenge to print the number that you would get if you convert a given number to binary, invert its bits, then convert back to decimal. I decided to give it a run in clojurescript (which I ...
0
votes
1answer
182 views

Find the product of digits at odd/even places

The 'score' of a race is given. We have to calculate the 'bonus points' to be given to the participant. The '...
40
votes
5answers
32k views

Incrementing Integers in Map

I have a HashMap<Token, Integer>, which counts occurrences of Token. Each time Token ...
2
votes
1answer
86 views

Using BigInt in JavaScript, write a factorial function that takes either a Number or BigInt and returns a BigInt

Is there anything missing or any pitfall when using BigInt? ...
4
votes
1answer
74 views

Binary Trie in F#

I have this little exercise writing a binary trie in F#. In C# it can be done in few lines, but here it became more complicated than I had expected (especially the ...
3
votes
1answer
69 views

Python maintain precision for currency

Working with currencies I am aware it is not good to work with floating point values due to rounding and maintaining precision. Better is to work with integers. Would the following Currency class make ...
2
votes
2answers
80 views

Iterating over a range of integer ranges in Java

My use case is this: RangeDemo.java: ...
3
votes
2answers
112 views

Test if a number is within 2 of a multiple of 10

I have solved this exercise, but it looks really redundant. Is there a built-in function in Java or a cleaner way to caluclate mod of a value + deviation without multiple OR statements? If I had to ...
2
votes
1answer
32 views

Indeterminate-length big-endian to little-endian conversion in Python

I have a need to convert wire encoding (big-endian) to little-endian in Python. The conversion needs to happen as a separate function; I am not allowed to change the function that reads the byte ...
5
votes
2answers
124 views

Templated division by a power of 2

I've implemented a div_by_power_of_2() function, which lets me force the compiler to use left-shifting rather than proper division, in cases when the developer know ...
13
votes
1answer
986 views

128-bit unsigned integer

This implements 128-bit unsigned integer using C++14. It works on MSVC and 32-bit architectures being complementary to the unsigned __int128 type provided by GCC ...
2
votes
2answers
98 views

BigInteger check in C++ from a string

You might consider this question a follow-up of: BigInteger check in C from a string Although, this time, I used C++. I am on Linux Mint 19.1 with the compiler version: ...
7
votes
2answers
187 views

Register “%b” conversion specifier

I'm writing a library to register the b, B conversion specifiers and make them work the closest possible to ...
4
votes
1answer
273 views

Converting long values in Java to unsigned strings

This one is a research attempt to find out how to convert long values as unsigned long integers to Strings. It is much slower ...
6
votes
1answer
96 views

Calculating the cube root

Inspired by a question on SO I was checking how to calculate the cube root of an integer and found a C-ish solution on Hacker's Delight: ...
4
votes
1answer
50 views

Program containing several bitset based classes including chars, strings, ints and a base bitset class

This is by far the biggest project i've ever made. The main goal was just to make a bitset class but it expanded into making several classes based off of that main bitset class. Some things I would ...
2
votes
3answers
163 views

Armstrong number generator

This is my code to generate all possible Armstrong numbers between the two given numbers. The logic uses string instead of integer to separate the digits to optimize the code. ...
-2
votes
2answers
174 views

ArmstrongNumber checker

This is my code written to check whether a given number is armstrong or not . but the logic differs from the conventional logic of using integer and separating its digits and calculating , instead i ...
4
votes
2answers
105 views

Defining a class that behaves exactly like an `int` [closed]

This is an exercise to create a class that behaves exactly like an int. Questions Am I missing anything? Or did I going about this about this the wrong way? Is ...
2
votes
1answer
41 views

Determine if a String can be parsed to LocalDateTime or Int

I have the following kotlin function that takes in a string "value", then attempts to parse "value" as both a LocalDateTime and an Int. It returns a Pair of the parsed value of "value" and an enum ...
3
votes
2answers
247 views

Reverse int within the 32-bit signed integer range: \$[−2^{31}, 2^{31} − 1]\$ Optimized

LeetCode Problem Reverse digits of a 32-bit signed integer. When the reversed integer overflows return 0. Feedback Optimized from beta code in the original question here. Based on this LeetCode ...
3
votes
1answer
531 views

Java BigInteger implementation

I've written a java application that can handle operations on big integers implemented with array list of digits. I have a difficulty with the way I implemented the divide method, because when the ...
4
votes
1answer
198 views

Reverse int within the 32-bit signed integer range

Problem Reverse digits of a 32-bit signed integer. When the reversed integer overflows return 0. Optimized code here. Feedback I'm looking for any ways I can optimize this with modern C++ ...
-3
votes
1answer
58 views

Code creates huge Text File [closed]

A Java program that writes each digit on a new line, from 1 to max integer. We should end up with 2.4 Billion lines. After looping to near the maximum integer, the text file becomes really huge. Like ...
8
votes
1answer
446 views

Prime factors and perfect square

I want to convert an integer to a perfect square by multiplying it by some number. That number is the product of all the prime factors of the number which not appear an even number of times. Example ...
3
votes
2answers
289 views

Adding thousand separator to various number types

Am making a pure .net library with helper functions (GitHub). However I wanted to have a thousand separator for all number types and here is what I am currently doing ...
4
votes
3answers
860 views

Haskell functions to extract and reverse integers hiding in strings

There may not be a "right" way to do it, but I've come to perceive Haskell as the kind of language, where literacy of the proposed programming paradigm is crucial to writing proficient code in the ...
1
vote
3answers
122 views

Functions to handle big numbers in C

I'm working to implement some simple bigint functions for addition, multiplication and powers. They work correctly but the larger the numbers the longer the time it takes. For example 210000 takes a ...
7
votes
4answers
726 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 ...
6
votes
3answers
253 views

Encoding 0/1 bits to 0/255 bytes in C++

I need to map a sequence of bits that represents a float into a sequence of bytes by this rule: the value 1 maps to the value 255 and the value 0 maps to the value 0. This is the code I have now: <...
3
votes
2answers
332 views

Change Arithmetic Right Shift to Logical Right Shift

The following code is a solution to a textbook (Bryant&O'Hallaron: Computer Systems A programmer's Perspective 2nd Ed) problem in bit-level data manipulation (attempted for the challenge, not a ...
0
votes
1answer
67 views

Breaking a 32-bit value into bytes for an ArrayList

I am very new to Java and its collections and I'm trying to figure out "best" way to populate an ArrayList<Byte> in Java. In particular, I'm trying to take a ...
9
votes
2answers
4k views

Int to Enum Extension Method

As you know, converting an int to an enum is straightforward. Doing it generically with type/range checking isn't. I looked for a reusable example since this seems like a common situation but did not ...
4
votes
2answers
89 views

BigInteger check in C from a string

I have started to learn C. At the time of writing I don't know anything about the language. The following I wrote today in a few hours. Though I do have a lot of programming experience, I find C hard ...
0
votes
1answer
56 views

Calculating the volume of a box

I've been studying more C using C Programming: A Modern Approach: A Modern Approach 2nd Edition by K.N. King as well as reading the comp.lang.c FAQ and the Code Review section here on Stack Exchange ...
4
votes
2answers
121 views

Test whether a number is a Harshad Number

A little code I wrote that will check if the entered 4-digit number is Harshad or not (Harshad number is one which divided by the sum of its digits will leave no remainder, special Harshad is the same ...
1
vote
1answer
165 views

Addition function for two Big Integers represented as std::string

This function is from my attempt to write a Big Integer class in C++. Below, I have a simple addition function. It takes two strings of arbitrary length which contains only digits (no '-' sign as ...
3
votes
2answers
78 views

Expressing positive integers as the sum of different charming numbers

In a practice academic interview of mine, we discussed question six, round one, of the United Kingdom Mathematics Trust's 2015 British Mathematical Olympiad. Which states: A positive integer is ...