number-theory

Represent natural number as sum of distinct squares

我怕爱的太早我们不能终老 提交于 2019-11-30 06:52:05
The problem is to find the largest set S of positive integers such that the sum of the squares of the elements of S is equal to a given number n. For example: 4 = 2² 20 = 4² + 2² 38 = 5² + 3² + 2² 300 = 11² + 8² + 7² + 6² + 4² + 3² + 2² + 1². I have an algorithm that runs in time O(2^(sqrt n) * n) , but it's too slow (every subset of squares). There's an O(n^1.5) -time algorithm based on the canonical dynamic program for subset sum . Here's the recurrence: C(m, k) is the size of the largest subset of 1..k whose squares sum to m C(m, k), m < 0 = -infinity (infeasible) C(0, k) = 0 C(m, 0), m > 0

Algorithm for finding smallest number with given number of factors

十年热恋 提交于 2019-11-30 06:47:28
What's the most efficient algorithm anyone can think of that, given a natural number n , returns the least natural number x with n positive divisors (including 1 and x )? For example, given 4 the algorithm should result in 6 (divisors: 1,2,3,6); i.e. 6 is the smallest number having 4 distinct factors. Similarly, given 6, the algorithm should result in 12 (divisors: 1,2,3,4,6,12); i.e. 12 is the smallest number having 6 distinct factors In terms of real-world performance, I'm looking for a scalable algorithm which can give answers of the order of 10 20 within 2 seconds on a machine which can do

How to compute a^^b mod m?

孤街浪徒 提交于 2019-11-30 03:58:22
I have to compute efficiently a^^b mod m for large values of a,b,m<2^32 where ^^ is the tetration operator: 2^^4=2^(2^(2^2)) m is not a prime number and not a power of ten. Can you help? To be clear, a^^b is not the same thing as a^b, it is the exponential tower a^(a^(a^...^a)) where there are b copies of a, also known as tetration. Let T(a,b) = a^^b so T(a,1) = a and T(a,b) = a^T(a,b-1). To compute T(a,b) mod m = a^T(a,b-1) mod m, we want to compute a power of a mod m with an extremely large exponent. What you can use is that modular exponentiation is preperiodic with preperiod length at most

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

浪子不回头ぞ 提交于 2019-11-29 18:41:17
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 function/program would return: 123 - 3 times 234 - 3 times 345 - 2 times They did not give me the solution

Create faster Fibonacci function for n > 100 in MATLAB / octave

倖福魔咒の 提交于 2019-11-29 12:35:21
问题 I have a function that tells me the nth number in a Fibonacci sequence. The problem is it becomes very slow when trying to find larger numbers in the Fibonacci sequence does anyone know how I can fix this? function f = rtfib(n) if (n==1) f= 1; elseif (n == 2) f = 2; else f =rtfib(n-1) + rtfib(n-2); end The Results, tic; rtfib(20), toc ans = 10946 Elapsed time is 0.134947 seconds. tic; rtfib(30), toc ans = 1346269 Elapsed time is 16.6724 seconds. I can't even get a value after 5 mins doing

Represent natural number as sum of distinct squares

社会主义新天地 提交于 2019-11-29 08:43:27
问题 The problem is to find the largest set S of positive integers such that the sum of the squares of the elements of S is equal to a given number n. For example: 4 = 2² 20 = 4² + 2² 38 = 5² + 3² + 2² 300 = 11² + 8² + 7² + 6² + 4² + 3² + 2² + 1². I have an algorithm that runs in time O(2^(sqrt n) * n) , but it's too slow (every subset of squares). 回答1: There's an O(n^1.5) -time algorithm based on the canonical dynamic program for subset sum. Here's the recurrence: C(m, k) is the size of the

Algorithm for finding smallest number with given number of factors

谁说胖子不能爱 提交于 2019-11-29 06:48:27
问题 What's the most efficient algorithm anyone can think of that, given a natural number n , returns the least natural number x with n positive divisors (including 1 and x )? For example, given 4 the algorithm should result in 6 (divisors: 1,2,3,6); i.e. 6 is the smallest number having 4 distinct factors. Similarly, given 6, the algorithm should result in 12 (divisors: 1,2,3,4,6,12); i.e. 12 is the smallest number having 6 distinct factors In terms of real-world performance, I'm looking for a

How does this prime number test in Java work?

天大地大妈咪最大 提交于 2019-11-29 04:04:43
The code snippet below checks whether a given number is a prime number. Can someone explain to me why this works? This code was on a study guide given to us for a Java exam. public static void main(String[] args) { int j = 2; int result = 0; int number = 0; Scanner reader = new Scanner(System.in); System.out.println("Please enter a number: "); number = reader.nextInt(); while (j <= number / 2) { if (number % j == 0) { result = 1; } j++; } if (result == 1) { System.out.println("Number: " + number + " is Not Prime."); } else { System.out.println("Number: " + number + " is Prime. "); } } Overall

Calculating sum of geometric series (mod m)

百般思念 提交于 2019-11-28 19:34:14
I have a series S = i^(m) + i^(2m) + ............... + i^(km) (mod m) 0 <= i < m, k may be very large (up to 100,000,000), m <= 300000 I want to find the sum. I cannot apply the Geometric Progression (GP) formula because then result will have denominator and then I will have to find modular inverse which may not exist (if the denominator and m are not coprime). So I made an alternate algorithm making an assumption that these powers will make a cycle of length much smaller than k (because it is a modular equation and so I would obtain something like 2,7,9,1,2,7,9,1....) and that cycle will

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

三世轮回 提交于 2019-11-28 07:52:18
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 is valid according to its size, before handling the message. For example: I know that a message contains