modulo

Division ( / ) not giving my answer in postgresql

痞子三分冷 提交于 2019-12-02 21:37:05
I have a table software and columns in it as dev_cost , sell_cost . If dev_cost is 16000 and sell_cost is 7500. How do I find the quantity of software to be sold in order to recover the dev_cost ? I have queried as below: select dev_cost / sell_cost from software ; It is returning 2 as the answer. But we need to get 3, right? What would be the query for that? Thanks in advance. Ilmari Karonen Your columns have integer types, and integer division truncates the result towards zero . To get an accurate result, you'll need to cast at least one of the values to float or decimal : select cast(dev

understanding the fizz buzz in C# [closed]

别来无恙 提交于 2019-12-02 20:31:48
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . When solving "fizz-buzz" in C# using a "while" loop, I found out that first I should find the multiples of both 3 and 5 (multiples of

Fastest modular exponentiation in JavaScript

左心房为你撑大大i 提交于 2019-12-02 18:30:15
My problem is to compute (g^x) mod p quickly in JavaScript, where ^ is exponentiation, mod is the modulo operation. All inputs are nonnegative integers, x has about 256 bits, and p is a prime number of 2048 bits, and g may have up to 2048 bits. Most of the software I've found that can do this in JavaScript seems to use the JavaScript BigInt library ( http://www.leemon.com/crypto/BigInt.html ). Doing a single exponentiation of such size with this library takes about 9 seconds on my slow browser (Firefox 3.0 with SpiderMonkey). I'm looking for a solution which is at least 10 times faster. The

Maximum subarray sum modulo M

試著忘記壹切 提交于 2019-12-02 16:16:18
Most of us are familiar with the maximum sum subarray problem . I came across a variant of this problem which asks the programmer to output the maximum of all subarray sums modulo some number M. The naive approach to solve this variant would be to find all possible subarray sums (which would be of the order of N^2 where N is the size of the array). Of course, this is not good enough. The question is - how can we do better? Example: Let us consider the following array: 6 6 11 15 12 1 Let M = 13. In this case, subarray 6 6 (or 12 or 6 6 11 15 or 11 15 12) will yield maximum sum ( = 12 ). We can

Surprising result from Math.pow(65,17) % 3233

丶灬走出姿态 提交于 2019-12-02 16:15:04
问题 For some reason when dealing with large numbers, the modulus operator doesnt give me the correct output, have a look at the code double x = Math.pow(65,17) % 3233; The output is supposed to be 2790 But the output is 887.0 I am sure its something silly but i cant get around it. Thanks in advance 回答1: The result of Math.pow(65, 17) cannot be represented exactly as a double , and is getting rounded to the nearest number that can. The pow(a, b) % c operation is called "modular exponentiation".

modulo operation on negative numbers [duplicate]

纵饮孤独 提交于 2019-12-02 16:14:43
问题 This question already has answers here : Modulo operation with negative numbers (12 answers) Closed 2 years ago . A modulo operation a%b returns the remainder for a/b but for negative numbers it does not do so. #include <stdio.h> int main(void) { int n=-4; printf("%d\n",n%3); return 0; } It should return 2 as 3*(-2)=-6 is just smaller than -4 and a multiple of 3 but the output is -1. Why is it treating (-a) mod b same as -(a mod b) 回答1: As a general rule, the modulo and division should

How to use mod operator in bash?

流过昼夜 提交于 2019-12-02 14:19:51
I'm trying a line like this: for i in {1..600}; do wget http://example.com/search/link $i % 5; done; What I'm trying to get as output is: wget http://example.com/search/link0 wget http://example.com/search/link1 wget http://example.com/search/link2 wget http://example.com/search/link3 wget http://example.com/search/link4 wget http://example.com/search/link0 But what I'm actually getting is just: wget http://example.com/search/link Mark Longair Try the following: for i in {1..600}; do echo wget http://example.com/search/link$(($i % 5)); done The $(( )) syntax does an arithmetic evaluation of

How to Calculate (a/b) %c where a,b and c are very large numbers [closed]

半腔热情 提交于 2019-12-02 13:16:35
I have a function f(x)=(1^1)*(2^2)*(3^3)*.....(x^x) i have to calculate (f(x)/(f(x-r)*f(r)))modulo c i can calculate f(x) and (f(x-r)*f(r)). assume f(x) is a and f(x-r)*f(r) is b. c is some number that is very larger. `` so i how can calculate (a/b)%c your f(x) is just ᴨ (PI cumulative multiplication) squared it is hard to write it in here so i will deifine g(x0,x1) instead g(x0,x1)=x0*(x0+1)*(x0+2)*...*x1 so: f(x)=g(1,x)^2 computing h(x,r,c)=f(x)/(f(x-r)*f(r))%c when you rewrite it to g() you get: h(x,r,c)=((g(1,x)/(g(1,x-r)*g(1,r)))^2)%c now simplify (and lower magnitude) as much as you can

mathematics behind modulo behavor

蓝咒 提交于 2019-12-02 11:44:07
Preamble This question is not about the behavior of (P)RNG and rand() . It's about using power of two values uniformly distributed against modulo. Introduction I knew that one should not use modulo % to convert a value from a range to another, for example to get a value between 0 and 5 from the rand() function: there will be a bias. It's explained here https://bitbucket.org/haypo/hasard/src/ebf5870a1a54/doc/common_errors.rst?at=default and in this answer Why do people say there is modulo bias when using a random number generator? But today after investigating some code which was looking wrong,

Math behind “compute n! under modulo p”?

拥有回忆 提交于 2019-12-02 09:23:08
long long x; for (int i = 1; i <= n; i++) { x = (x * i) % m; } cout << x; This is the trick to calculate (n!) mod m (assume m > n). However, I don't know why it's true. Can you explain the math mechanism behind this? The basic idea here is that you can take the modulus before, during, or after multiplication and get the same value after taking the modulus of the final result. As @Peter points out, (a * b) % m == ((a % m) * (b % m)) % m For the factorial, n! = 1 * 2 * 3 * ... * (n-1) * n so we have n! % m = (((((((1 * 2) % m) * 3) % m) * ... * n-1) % m) * n) % m taking the modulus after each