integer-arithmetic

Java Arithmetic division

匆匆过客 提交于 2019-12-08 08:08:18
问题 public class test { public static void main(String[] args) { int total = 2; int rn = 1; double rnp = (rn / total) * 100; System.out.println(rnp); } } Why it prints 0.0 instead of 50.0? https://www.google.com/search?q=100*(1%2F2)&aq=f&oq=100*(1%2F2) 回答1: The division occurs in integer space with no notion of fractions, you need something like double rnp = (rn / (double) total) * 100 回答2: You are invoking integer division here (rn / total) Integer division rounds towards zero. Try this instead:

How to optimize printing out the difference between the greater and the lesser of two integers?

独自空忆成欢 提交于 2019-12-07 08:41:03
问题 UVA Problem no. 10055, Hashmat the Brave Warrior, probably the easiest problem there. The input consists of a series of pairs of unsigned integers ≤ 2^32 (thus mandating the use of 64bit integers…) For each pair the task is to print out the difference between the greater and the lesser integer. According to the statistics, the fastest solutions run in below 0.01 sec. However, all my attempts to solve this typically run in 0.02 sec, with probably random deviations of ± 0.01 sec. I tried:

How to optimize printing out the difference between the greater and the lesser of two integers?

China☆狼群 提交于 2019-12-05 16:03:08
UVA Problem no. 10055, Hashmat the Brave Warrior , probably the easiest problem there. The input consists of a series of pairs of unsigned integers ≤ 2^32 (thus mandating the use of 64bit integers…) For each pair the task is to print out the difference between the greater and the lesser integer. According to the statistics , the fastest solutions run in below 0.01 sec. However, all my attempts to solve this typically run in 0.02 sec, with probably random deviations of ± 0.01 sec. I tried: #include <cstdint> #include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(false);

How to perform ceiling-division in integer arithmetic? [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-04 03:19:22
问题 This question already has answers here : Is there a ceiling equivalent of // operator in Python? (7 answers) Closed 4 years ago . It's basically returning the boxes_needed. 1 box can contain 10 items. So if the items typed by the user is 102 then the code should return 11 boxes. Is there a way to divide that rounds upwards if there is a non-zero remainder? 回答1: For your use case, use integer arithmetic. There is a simple technique for converting integer floor division into ceiling division:

From a loop index k, obtain pairs i,j with i < j?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 16:40:23
I need to traverse all pairs i,j with 0 <= i < n , 0 <= j < n and i < j for some positive integer n . Problem is that I can only loop through another variable, say k . I can control the bounds of k . So the problem is to determine two arithmetic methods, f(k) and g(k) such that i=f(k) and j=g(k) traverse all admissible pairs as k traverses its consecutive values. How can I do this in a simple way? I think I got it (in Python): def get_ij(n, k): j = k // (n - 1) # // is integer (truncating) division i = k - j * (n - 1) if i >= j: i = (n - 2) - i j = (n - 1) - j return i, j for n in range(2, 6):

Find the a 4 digit number who's square is 8 digits AND last 4 digits are the original number [closed]

∥☆過路亽.° 提交于 2019-12-03 14:55:50
Closed . This question needs to be more focused. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it focuses on one problem only by editing this post . From the comments on my answer here , the question was asked (paraphrase): Write a Python program to find a 4 digit whole number, that when multiplied to itself, you get an 8 digit whole number who's last 4 digits are equal to the original number. I will post my answer, but am interested in a more elegant solutions concise but easily readable solution! (Would someone new-ish to python be

Is masking before unsigned left shift in C/C++ too paranoid?

情到浓时终转凉″ 提交于 2019-12-03 03:27:51
问题 This question is motivated by me implementing cryptographic algorithms (e.g. SHA-1) in C/C++, writing portable platform-agnostic code, and thoroughly avoiding undefined behavior. Suppose that a standardized crypto algorithm asks you to implement this: b = (a << 31) & 0xFFFFFFFF where a and b are unsigned 32-bit integers. Notice that in the result, we discard any bits above the least significant 32 bits. As a first naive approximation, we might assume that int is 32 bits wide on most platforms

Determining if a number is either a multiple of ten or within a particular set of ranges

99封情书 提交于 2019-12-03 03:23:06
问题 I have a few loops that I need in my program. I can write out the pseudo code but I'm not entirely sure how to write them logically. I need - if (num is a multiple of 10) { do this } if (num is within 11-20, 31-40, 51-60, 71-80, 91-100) { do this } else { do this } //this part is for 1-10, 21-30, 41-50, 61-70, 81-90 This is for a snakes and ladders board game, if it makes any more sense for my question. I imagine the first if statement I'll need to use modulus, would if (num == 100%10) be

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

China☆狼群 提交于 2019-12-03 01:53:44
问题 The following Python 3.x integer multiplication takes on average between 1.66s and 1.77s: import time start_time = time.time() num = 0 for x in range(0, 10000000): # num += 2 * (x * x) num += 2 * x * x print("--- %s seconds ---" % (time.time() - start_time)) if I replace 2 * x * x with 2 *(x * x) , it takes between 2.04 and 2.25 . How come? On the other hand it is the opposite in Java: 2 * (x * x) is faster in Java. Java test link: Why is 2 * (i * i) faster than 2 * i * i in Java? I ran each

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

丶灬走出姿态 提交于 2019-12-02 17:16:46
The following Python 3.x integer multiplication takes on average between 1.66s and 1.77s: import time start_time = time.time() num = 0 for x in range(0, 10000000): # num += 2 * (x * x) num += 2 * x * x print("--- %s seconds ---" % (time.time() - start_time)) if I replace 2 * x * x with 2 *(x * x) , it takes between 2.04 and 2.25 . How come? On the other hand it is the opposite in Java: 2 * (x * x) is faster in Java. Java test link: Why is 2 * (i * i) faster than 2 * i * i in Java? I ran each version of the program 10 times, here are the results. 2 * x * x | 2 * (x * x) ------------------------