greatest-common-divisor

BigIntegers, gcd , modulus inverse to find public key

狂风中的少年 提交于 2020-01-02 17:20:53
问题 So, Im using java to find the public key of a RSA password. Right now Im unsure what I'm doing and also if it's correct. I have this information for the public key. C = 5449089907 n = p*q = 8271344041 q = 181123 p = n/q = 45667 d = 53 phi(n) = (p-1)(q-1) = 8271117252 What complicates things are the BigIntegers, the numbers are way to huge for int and longs, so I have to use the clumsy BigIntegers. As far as I understand I have the following equation to solve. e*5198987987 - x*8271117252 = 1 I

Using Prolog to compute the GCD of a polynomial

和自甴很熟 提交于 2019-12-31 03:51:25
问题 The title kind of says it all. I'm looking to compute the GCD of two polynomials. Is there any way this can be done in Prolog? If so, what's a good starting point? Specifically, I'm having trouble with how to implement polynomial division using Prolog. Edit to include example input and output: Example input: ?- GCD(x^2 + 7x + 6, x2 − 5x − 6, X). Example output: X = x + 1. Solution On the off chance that someone else needs to do this, here's my final solution: tail([_|Tail], Tail). head([Head

Implementation of the in-built __gcd method in C++

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 15:51:16
问题 Does the in-built __gcd method in stl-algorithm library use Euclid's algorithm in its implementation? 回答1: The source code seems to be /** * This is a helper function for the rotate algorithm specialized on RAIs. * It returns the greatest common divisor of two integer values. */ template<typename _EuclideanRingElement> _EuclideanRingElement __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n) { while (__n != 0) { _EuclideanRingElement __t = __m % __n; __m = __n; __n = __t; } return __m

Finding gcd of permutations of a Number

房东的猫 提交于 2019-12-23 05:21:17
问题 Here is the link to the problem: http://www.spoj.com/problems/GCD/ Consider the decimal representation of a natural number N. Find the greatest common divisor (GCD) of all numbers that can be obtained by permuting the digits in the given number. Leading zeroes are allowed. I worked on the following approach : https://math.stackexchange.com/a/22453 First, if all the digits are the same, there is only one number and that is the GCD. As was pointed out before, if 3 or 9 is a factor of one

Java: Get Greatest Common Divisor, which method is better?

天涯浪子 提交于 2019-12-22 07:57:11
问题 From this question Java: get greatest common divisor In getting the gcd of any data type whether int , long , Integer , Long , which answer is better in terms of precision, speed, cpu usage, etc.? A: private static int gcdThing(int a, int b) { return BigInteger.valueOf(a).gcd(BigInteger.valueOf((b))).intValue(); } B: public int GCD(int a, int b) { return b==0 ? a : GCD(b, a%b); } 回答1: Random r = new Random(); int[] ints = new int[500000]; for (int i = 0 ; i < ints.length ; i++) ints[i] = r

How to calculate Least common multiple of {1, 2, 3, …, n}?

六月ゝ 毕业季﹏ 提交于 2019-12-21 16:51:26
问题 How to find LCM of {1, 2, ..., n} where 0 < n < 10001 in fastest possible way. The one way is to calculate n! / gcd (1,2,.....,n) but this can be slow as number of testcases are t < 501 and the output should be LCM ( n! ) % 1000000007 Code for the same is: #include<bits/stdc++.h> using namespace std; #define p 1000000007; int fact[10001] = {1}; int gcd[10001] = {1}; int main() { int i, j; for( i = 2;i < 10001; i++){ fact[i] = ( i * fact[i-1]) % p; } for(i=2 ;i < 10001; i++){ gcd[i] =__gcd(

RSA: Private key calculation with Extended Euclidean Algorithm

主宰稳场 提交于 2019-12-21 03:18:14
问题 I'm a high school student writing a paper on RSA, and I'm doing an example with some very small prime numbers. I understand how the system works, but I can't for the life of me calculate the private key using the extended euclidean algorithm. Here's what I have done so far: I have chosen the prime numbers p=37 and q=89 and calculated N=3293 I have calculated (p-1)(q-1)=3168 I have chosen a number e so that e and 3168 are relatively prime. I'm checking this with the standard euclidean

How to find sum of all the GCDs of all the subarrays of an array?

血红的双手。 提交于 2019-12-19 11:54:42
问题 Given an array A of length n = 10^5. I have to find the sum of GCD of all subarrays of this array efficiently. import math def lgcd(a): g = a[0] for i in range(1,len(a)): g = math.gcd(g,a[i]) return g n = int(input()) A = list(map(int,input().split())) ans = 0 for i in range(n): for j in range(i,n): ans+=lgcd(A[i:j+1]) print(ans) 回答1: The first thing that should be marked is that GCD(A[i-1 : j]) * d = GCD(A[i : j]) where d is natural number. So for the fixed subarray end, there will be some

GCD function in c++ sans cmath library

让人想犯罪 __ 提交于 2019-12-17 22:45:53
问题 I'm writing a mixed numeral class and need a quick and easy 'greatest common divisor' function. Can anyone give me the code or a link to the code? 回答1: I'm tempted to vote to close -- it seems difficult to believe that an implementation would be hard to find, but who knows for sure. template <typename Number> Number GCD(Number u, Number v) { while (v != 0) { Number r = u % v; u = v; v = r; } return u; } In C++ 17 or newer, you can just #include <numeric> , and use std::gcd (and if you care,

GCD of an array

微笑、不失礼 提交于 2019-12-14 03:32:06
问题 You are given an array A of integers of size N. You will be given Q queries where each query is represented by two integers L, R. You have to find the gcd(Greatest Common Divisor) of the array after excluding the part from range L to R inclusive MY Approach : public static int gcd(int a ,int b) { if(b == 0) return a; return gcd(b, a % b); } for(int j = 0; j < Q; j++) { int l = in.nextInt(); int r = in.nextInt(); ans = 0; for(int k = 1; k <= n; k++) { if(k < l || k > r) ans = gcd(a[k], ans); }