number-theory

Fastest way to find all the divisors of a large number

▼魔方 西西 提交于 2021-02-08 07:35:36
问题 I am facing a problem when I want to get all the divisors of a large number i.e n=10^12. There is a method, which checks all possible numbers less than square root of given number n. for(int i=1; i<=sqrt(n); ++i){ if(n%i==0){ factors.push_back(i); if(i!=n/i) factors.push_back(n/i); } } But when n is very large i.e 10^12 then it needs 10^6 iterations which is very slow. Is there any faster way when I have all prime divisors of given n such as given number is 48. Then prime factorization of 720

how to calculate a^(b^c) mod n? [duplicate]

时光毁灭记忆、已成空白 提交于 2021-02-08 04:37:27
问题 This question already has answers here : finding a^b^c^… mod m (6 answers) The most efficient way to implement an integer based power function pow(int, int) (17 answers) Closed 6 years ago . Can someone tell me the efficient way to solve this problem ? a b c mod m a, b, c, and m are 32 bit int where m is not a prime number and a is not coprime to m ?I've looking for the answer, but haven't found it There are some answered questions that familiar with this question, but none of them can

For a given value of n and m, find fib(n) mod m where n is very huge. (Pisano Period)

故事扮演 提交于 2021-02-04 16:43:35
问题 Input Integers 'n' (up to 10^14) and 'm'(up to 10^3) Output Fib(n) modulo m Sample Cases Input: 239 1000 Output: 161 Input: 2816213588 239 Output: 151 Hint given in Question As it is not possible to iterate 'n' times (because n is huge), consider using Pisano Period(repetition of remainders when every element Fibonacci series is divided by any integer) Code which I wrote (maybe wrong, but passes above-mentioned cases) n, m = map(int, input().split()) a, b = 0, 1 fib_rems = [0, 1] # remainders

Digit wise modulo for calculating power function for very very large positive integers

青春壹個敷衍的年華 提交于 2020-01-03 06:20:06
问题 Hi I am writing a code to calculate P^Q where P, Q are positive integers which can have number of digits upto 100000 I want the result as result = (P^Q)modulo(10^9+7) Example: P = 34534985349875439875439875349875 Q = 93475349759384754395743975349573495 Answer = 735851262 I tried using the trick: (P^Q)modulo(10^9+7) = (P*P*...(Q times))modulo(10^9+7) (P*P*...(Q times))modulo(10^9+7) = ((Pmodulo(10^9+7))*(Pmodulo(10^9+7))...(Q times))modulo(10^9+7) Since both P and Q are very large, I should

Digit wise modulo for calculating power function for very very large positive integers

。_饼干妹妹 提交于 2020-01-03 06:19:07
问题 Hi I am writing a code to calculate P^Q where P, Q are positive integers which can have number of digits upto 100000 I want the result as result = (P^Q)modulo(10^9+7) Example: P = 34534985349875439875439875349875 Q = 93475349759384754395743975349573495 Answer = 735851262 I tried using the trick: (P^Q)modulo(10^9+7) = (P*P*...(Q times))modulo(10^9+7) (P*P*...(Q times))modulo(10^9+7) = ((Pmodulo(10^9+7))*(Pmodulo(10^9+7))...(Q times))modulo(10^9+7) Since both P and Q are very large, I should

Given a string of a million numbers, return all repeating 3 digit numbers

我是研究僧i 提交于 2019-12-29 10:08:31
问题 I had an interview with a hedge fund company in New York a few months ago and unfortunately, I did not get the internship offer as a data/software engineer. (They also asked the solution to be in Python.) I pretty much screwed up on the first interview problem... Question: Given a string of a million numbers (Pi for example), write a function/program that returns all repeating 3 digit numbers and number of repetition greater than 1 For example: if the string was: 123412345123456 then the

Algorithm to determine non-negative-values solution existance for linear diophantine equation

孤街醉人 提交于 2019-12-28 12:44:28
问题 I am looking for a method to determine if there is a solution to equations such as: 3n1+4n2+5n3=456 , where n1,n2,n3 are positive integers. Or more general: are there zero or positive integers n1,n2,n3 ... that solves the equation k1n1+k2n2+k3n3...=m where k1,k2,k3 ... and m are known positive integers. I don't need to find a solution - just to determine if a solution exist. Edit: Concerning the practical use of this algorithm: In a communication library, I want to decide if a given message

how to find consecutive composite numbers in R

被刻印的时光 ゝ 提交于 2019-12-25 19:41:31
问题 I want first 'n' consecutive composite numbers I searched command for finding consecutive composite numbers, but i got the result proving for that thorem. I didn't get any command for that..please help me to slove this problem in R. 回答1: Here is another option: n_composite <- function(n) { s <- 4L i <- 1L vec <- numeric(n) while(i <= n) { if(any(s %% 2:(s-1) == 0L)) { vec[i] <- s i <- i + 1L } s <- s + 1L } vec } It uses basic control flows to cycle through positive integers indexing

how to find consecutive composite numbers in R

会有一股神秘感。 提交于 2019-12-25 19:41:30
问题 I want first 'n' consecutive composite numbers I searched command for finding consecutive composite numbers, but i got the result proving for that thorem. I didn't get any command for that..please help me to slove this problem in R. 回答1: Here is another option: n_composite <- function(n) { s <- 4L i <- 1L vec <- numeric(n) while(i <= n) { if(any(s %% 2:(s-1) == 0L)) { vec[i] <- s i <- i + 1L } s <- s + 1L } vec } It uses basic control flows to cycle through positive integers indexing

Boolean convolution using integer multiplication

两盒软妹~` 提交于 2019-12-24 14:16:44
问题 In algorithms presented in Bringmann16 article a Boolean convolution is suggested to use to get sumset for two sets of positive integers. In above work both sets are represented as bitmasks - indicator vectors. If represent them as polynomials in form 1 + bit(0) * x + bit(1) * x^2 + ... + bit(i) * x^(i + 1) + ... + bit(n - 1) * x^n , then indeed their product contains only those monomials, whose power is number either in first set, or in second set or in sumset. The coefficients of product