Can this script have better performance using modular exponentiation?

人走茶凉 提交于 2020-01-24 01:37:16
问题 def f(a, b, c): return ((a ** b)-1) // c % b Can this script be faster in some way? (I have been looking for something with modular exponentiation): pow(a, b, c) == a ** b % c but this above script doesn't seem to be improvable like that. Does anyone know a way to speedup the above script? Thanks in advance. Edit: The second script is not at all the same as the first one, it is just meant to show what kind of optimization I had in mind. Edit: I didn't put the exact equation in becouse I

How to implement modular exponentiation?

雨燕双飞 提交于 2020-01-05 05:42:12
问题 I am trying to calculate something like this: a^b mod c, where all three numbers are large. Things I've tried: Python's pow() function is taking hours and has yet to produce a result. (if someone could tell me how it's implemented that would be very helpful!) A right-to-left binary method that I implemented, with O(log e) time, would take about 30~40 hours (don't wanna wait that long). Various recursion methods are producing segmentation faults (after I changed the recursion limits) Any

Finding binomial coefficient for large n and k modulo m

懵懂的女人 提交于 2019-12-30 06:58:08
问题 I want to compute nCk mod m with following constraints: n<=10^18 k<=10^5 m=10^9+7 I have read this article: Calculating Binomial Coefficient (nCk) for large n & k But here value of m is 1009. Hence using Lucas theorem, we need only to calculate 1009*1009 different values of aCb where a,b<=1009 How to do it with above constraints. I cannot make a array of O(m*k) space complexity with given constraints. Help! 回答1: Just use the fact that (n, k) = n! / k! / (n - k)! = n*(n-1)*...*(n-k+1)/[k*(k-1)

Built-in mod ('%') vs custom mod function: improve the performance of modulus operation

折月煮酒 提交于 2019-12-29 04:03:47
问题 Recently I came to know that the mod('%') operator is very slow. So I made a function which will work just like a%b. But is it faster than the mod operator? Here's my function int mod(int a, int b) { int tmp = a/b; return a - (b*tmp); } 回答1: According to Chandler Carruth's benchmarks at CppCon 2015, the fastest modulo operator (on x86, when compiled with Clang) is: int fast_mod(const int input, const int ceil) { // apply the modulo operator only when needed // (i.e. when the input is greater

Montgomery multiplication VHDL Implementation

若如初见. 提交于 2019-12-24 10:49:48
问题 I am trying to create a modular arithmetic operation in this case: x*y mod n As far as I have read the fastest way to do it is using the Montgomery multiplication, but I cant understand how that is actually done in other to implement it in hardware using VHDL. Has someone been able to do it or have any library that enables me to use it? 回答1: A basic shift and add modular multiplication can be found here as a part of this open cores project. Here's another project that is using the Montgomery

Elliptic curve addition in Jacobian coordinates

╄→尐↘猪︶ㄣ 提交于 2019-12-23 20:24:53
问题 I try to add two points on an elliptic curve over a prime field, converting these points from affine/to-affine coordinates, but do not manage to get a correct result (the curve I am testing has a=0). Anyone can see what's wrong? // From Affine BigInteger X1=P.x; BigInteger Y1=P.y; BigInteger Z1=BigInteger.ONE; BigInteger X2=Q.x; BigInteger Y2=Q.y; BigInteger Z2=BigInteger.ONE; // Point addition in Jacobian coordinates for a=0 // see

Adding elements of two big arrays in java

无人久伴 提交于 2019-12-23 04:58:05
问题 I have to come up with an algorithm that adds elements of two big arrays(size of each array is 10⁹ of integers that can go up to 10⁹). When declaring two arrays in java with size of 10⁹ each, I get a memory exception! The problem statement: 回答1: by analyzing the input constraints you can see that you can get 2*10^5 * 10^9 array accesses in the worst case if you implement the solution using two arrays of ints. So that approach will not do. If you somehow solve your MLE

Adding hexadecimal strings in JavaScript efficiently

蓝咒 提交于 2019-12-23 03:35:07
问题 In JavaScript, I have two variables that contain a hexadecimal number as a string, each. E.g.: var a = 'a3bc', b = '1d0f'; Now I want to add them (so, the result should be 'c0cb' ). To make things a little bit easier, let's put some constraints on this: The numbers always consist of the same number of digits (i.e., the strings are of same length). The numbers are prefixed with 0 s if necessary, so it will be '001a' , not just '1a' . On the other side, there are constraints that make things a

How to Convert from a Residual Number System to a Mixed Radix System?

冷暖自知 提交于 2019-12-21 20:48:28
问题 I understand the concept of a Residual Number System and the concept of a Mixed Radix system, but I'm having difficulty getting any of the conversion methods I find to work in a simple case study. I started at Knuth's Art of Computer Programming but that had a bit too much on the theory of the conversion, and once Euler was mentioned I was lost. Wikipedia has a nice section on the subject, which I tried here and here but both times I couldn't get back to the number where I started. I found a

Sympy: Solving Matrices in a finite field

家住魔仙堡 提交于 2019-12-18 16:25:08
问题 For my project, I need to solve for a matrix X given matrices Y and K. (XY=K) The elements of each matrix must be integers modulo a random 256-bit prime. My first attempt at solving this problem used SymPy's mod_inv(n) function. The problem with this is that I'm running out of memory with matrices of around size 30. My next thought was to perform matrix factorization, as that might be less heavy on memory. However, SymPy seems to contain no solver that can find matrices modulo a number. Any