Questions tagged [integer-arithmetic]

Anything related to integer arithmetic, i.e. arithmetic operations on integer numbers. This is mostly relevant for languages which represent integer numbers with specific data-types (e.g. `int` or `long` in C, C++ or Java).

Anything related to integer arithmetic, i.e. arithmetic operations on integer numbers. This is mostly relevant for languages which represent integer numbers with specific data-types (e.g. int or long in C, C++ or Java).

452 questions
-3
votes
1 answer

why n mod 10 in loop doesn't show output

I have solve a basic problem in c that is count the digits in integer and I have written - #include int main() { int n; scanf("%d",&n); int i; while(n!=0) { n %= 10; ++i; } …
-3
votes
2 answers

Arithmetic Exception integer

when i try to input this integer i get an error -10 20 -40 << error the output should be 80 { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String[] input = new String[n]; for (String input1 : input) { input =…
-3
votes
2 answers

How to calculate 1000 with eight '8' and +, -, *, /

This is my homework. I can't figure it out with an algorithm. Please help me. It's better to use C++/C. UPDATE: I'm sorry that I didn't describe this problem clearly. vivek_23: "I have assumed you meant to use 8 as is and use +,-,*,/ between them…
Jaycee Zhou
  • 69
  • 1
  • 5
-3
votes
1 answer

How implicit conversion work with signed character and unsigned int?

#include void main() { unsigned x = 1; signed char y = -1; if(x > y) printf("x > y"); else if(x == y) printf("x == y"); else printf("x < y"); printf("\n"); printf("%d",(signed char)x); …
-3
votes
2 answers

How to determine tiered tax rate with a concise snippet?

I am trying to create a tiered tax calculator. The user enters an amount, say 500,000 From that amount, I then have the following tiers that tax is charged as follows: 0 - 125,000 = 0% 125,001 - 250,000 = 2% 250,001 - 925,000 = 5% …
Holo
  • 579
  • 10
  • 26
-3
votes
1 answer

Is conversion of some data type to a larger data type the same as arithmetic right shift?

This is a two fold question . I have been reading up on the intricacies of how compilers process code and I am having this confusion. Both processes seem to be following the same logic of sign extension for signed integers. So is conversion simply…
surya
  • 253
  • 3
  • 9
-3
votes
1 answer

Implementing / enforcing wraparound arithmetic in C

The C standard says that overflow in arithmetic is undefined. I would like to know how to implement wraparound arithmetic in a performance-friendly way. This means that overflow checking solutions like presented here are not an option (as they slow…
Norswap
  • 11,740
  • 12
  • 47
  • 60
-3
votes
1 answer

How to prove the right identity in setoid in Agda

I wanted to prove group properties over integer. I found the setoid representation of integer makes proof easy. ℤ is defined as (ℕ , ℕ) such that (a , b) represents a - b zero : ℤ zero = 0 , 0 I have already proven leftIdZ : (a : ℤ) → zero + a ≡…
ajayv
  • 641
  • 6
  • 21
-3
votes
1 answer

Unexplainable Infinite Loop when trying to get the big number

I tried to find the biggest non-floating number in existance. However, the following Haskell Code did not work in GHCi: head (reverse [1..]) My pc is almost freezing when trying to compute the result. I tried to prove with mathematical induction…
Alexander Mills
  • 277
  • 3
  • 18
-3
votes
3 answers

Multiplying two integers if initial input is an odd integer in C++

I'm working on a homework assignment for a beginning C++ class and I'm a bit lost. Here's the assignment: Create a c++ program which ask the user to input a number. The output of the program should be one of the following: You entered an EVEN…
-3
votes
1 answer

Why is min() giving bizarre results?

While debugging a program in Visual Studio 2013, I found something strange. In one expression where I use min(), strange values appear. In the Immediate Window in VS, I try this: min(W, H) 35 '#' W 291 H 682 Duh-what?? What is the explanation of…
DarenW
  • 16,549
  • 7
  • 63
  • 102
-3
votes
2 answers

Is this possible to write in Dart the high performance code that works on 32-bit machines with 32-bit integers?

I was a little discouraged such a huge difference in performance when working on 32-bit machines with 32-bit integers in Dart language. Is this means that Dart VM still not optimized for integer arithmetic? Here is my pretty straightforward…
mezoni
  • 10,684
  • 4
  • 32
  • 54
-4
votes
1 answer

C++ Number theory: Fastest way to compute max(y = a_i * x+ b_i) <= k

following Problem, when having to make a fast code: I have a list of 2 integers a_i and b_i and I have to compute the equation: y = (a_i * x + b_i), where I'm only interested in y, not in x. All a_i's are prime and different from each other. a_i = y…
-5
votes
2 answers

Arithmetic optimization

How to optimize the following (convert arithmetic to bit-wise operations)? Optimize: int A = B * 4 int A = B * 72 int A = B % 1 int A = B % 16 int A = (B + C) / 2 int A = (B * 3) / 8 int A = (B % 8) * 4 Saw these questions in interview.
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
-5
votes
2 answers

Why does my code work? Simple arithmetics

I am writing a simple code to calculate Fabonacci numbers as an exercise. The code works, but i don't get why. I have some special cases for n=1 and n=2 which is the place of the number in the sequence (the numbers are 0 and 1). However after those,…
Martin Johnsrud
  • 161
  • 1
  • 6
1 2 3
30
31