number-theory

Why does array size have to be 3^k+1 for cycle leader iteration algorithm to work?

蹲街弑〆低调 提交于 2019-12-03 14:13:36
The cycle leader iteration algorithm is an algorithm for shuffling an array by moving all even-numbered entries to the front and all odd-numbered entries to the back while preserving their relative order. For example, given this input: a 1 b 2 c 3 d 4 e 5 the output would be a b c d e 1 2 3 4 5 This algorithm runs in O(n) time and uses only O(1) space. One unusual detail of the algorithm is that it works by splitting the array up into blocks of size 3 k +1. Apparently this is critical for the algorithm to work correctly, but I have no idea why this is. Why is the choice of 3 k + 1 necessary in

Given a permutation's lexicographic number, is it possible to get any item in it in O(1)

不打扰是莪最后的温柔 提交于 2019-12-03 10:57:23
问题 I want to know whether the task explained below is even theoretically possible, and if so how I could do it. You are given a space of N elements (i.e. all numbers between 0 and N-1 .) Let's look at the space of all permutations on that space, and call it S . The i th member of S , which can be marked S[i] , is the permutation with the lexicographic number i . For example, if N is 3, then S is this list of permutations: S[0]: 0, 1, 2 S[1]: 0, 2, 1 S[2]: 1, 0, 2 S[3]: 1, 2, 0 S[4]: 2, 0, 1 S[5]

How to enumerate x^2 + y^2 = z^2 - 1 (with additional constraints)

笑着哭i 提交于 2019-12-03 09:19:01
问题 Lets N be a number (10<=N<=10^5) . I have to break it into 3 numbers (x,y,z) such that it validates the following conditions. 1. x<=y<=z 2. x^2+y^2=z^2-1; 3. x+y+z<=N I have to find how many combinations I can get from the given numbers in a method. I have tried as follows but it's taking so much time for a higher number and resulting in a timeout.. int N= Int32.Parse(Console.ReadLine()); List<String> res = new List<string>(); //x<=y<=z int mxSqrt = N - 2; int a = 0, b = 0; for (int z = 1; z

What is the fastest way to check if two given numbers are coprime?

吃可爱长大的小学妹 提交于 2019-12-03 06:26:35
One way is to calculate their gcd and check if it is 1. Is there some faster way? The Euclidean algorithm (computes gcd ) is very fast. When two numbers are drawn uniformly at random from [1, n] , the average number of steps to compute their gcd is O(log n) . The average computation time required for each step is quadratic in the number of digits. There are alternatives that perform somewhat better (i.e., each step is subquadratic in the number of digits), but they are only effective on very large integers. See, for example, On Schönhage's algorithm and subquadratic integer gcd computation .

Given a permutation's lexicographic number, is it possible to get any item in it in O(1)

主宰稳场 提交于 2019-12-03 01:23:49
I want to know whether the task explained below is even theoretically possible, and if so how I could do it. You are given a space of N elements (i.e. all numbers between 0 and N-1 .) Let's look at the space of all permutations on that space, and call it S . The i th member of S , which can be marked S[i] , is the permutation with the lexicographic number i . For example, if N is 3, then S is this list of permutations: S[0]: 0, 1, 2 S[1]: 0, 2, 1 S[2]: 1, 0, 2 S[3]: 1, 2, 0 S[4]: 2, 0, 1 S[5]: 2, 1, 0 (Of course, when looking at a big N , this space becomes very large, N! to be exact.) Now, I

How does addition work in Computers?

眉间皱痕 提交于 2019-12-01 18:14:52
I was watching a video on computer architecture and a question came to my mind. How does addition and basic operations work on computers? I mean, i know that 2+2 = 4 but i don't know why? i just know that if i add 2 apples to another 2 then i see 4, but is there a possible demonstration of this? My question being is, how does a computer know that 2 + 2 = 4 at the most basic level? i know there are functions that add numbers, but at a basic level how is that addition performed? I just want to know this to understand better how computers work as the most basic and used operation performed by a

Efficient algorithm for finding a common divisor closest to some value?

*爱你&永不变心* 提交于 2019-12-01 03:37:40
I have two numbers, x1 and x2 . For a number y , I want to calculate the common divisor of x1 and x2 as close as possible to y . Is there an efficient algorithm for this? I believe it's time to rephrase my problem and be more clear . This is not about integers... So, say we have two numbers x1 and x2 . Say, the user inputs a number y . What I want to find, is a number y' close to y so that x1 % y' and x2 % y' are very small (smaller than 0.02 , for example, but lets call this number LIMIT ). In other words, I don't need an optimal algorithm, but a good approximation. I thank you all for your

Picking A, C and M for Linear congruential generator

风流意气都作罢 提交于 2019-11-30 13:44:29
问题 I am looking to implement a simple pseudorandom number generator (PRNG) that has a specified period and guaranteed no collisions for the duration of that period. After doing some research I came across the very famous LCG which is perfect. The problem is, I am having trouble understanding how to properly configure it. Here is my current implementation: function LCG (state) { var a = ?; var c = ?; var m = ?; return (a * state + c) % m; } It says that in order to have a full period for all seed

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

孤者浪人 提交于 2019-11-30 08:53:24
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 rtfib(100) PS: I'm using octave 3.8.1 If time is important (not programming techniques): function f = fib

Picking A, C and M for Linear congruential generator

别说谁变了你拦得住时间么 提交于 2019-11-30 08:36:05
I am looking to implement a simple pseudorandom number generator (PRNG) that has a specified period and guaranteed no collisions for the duration of that period. After doing some research I came across the very famous LCG which is perfect. The problem is, I am having trouble understanding how to properly configure it. Here is my current implementation: function LCG (state) { var a = ?; var c = ?; var m = ?; return (a * state + c) % m; } It says that in order to have a full period for all seed values the following conditions must be met: c and m are relatively prime a-1 is divisible by all