algorithm

Algorithm to find nth root of a number

不想你离开。 提交于 2021-02-16 04:34:51
问题 I am looking for an efficient algorithm to find nth root of a number. The answer must be an integer. I have found that newtons method and bisection method are popular methods. Are there any efficient and simple methods for integer output? 回答1: #include <math.h> inline int root(int input, int n) { return round(pow(input, 1./n)); } This works for pretty much the whole integer range (as IEEE754 8-byte double s can represent the whole 32-bit int range exactly, which are the representations and

Algorithm for generating a bracket model list in Python

点点圈 提交于 2021-02-15 11:06:45
问题 I'm trying to make a simple recursive function that will generate a list of nested lists in Python. The end result will represent a single elimination tournament bracket. I'm hoping that creating a list like this will make it easy for me to generate what I need. This will later be used to create models for tournament matches. So if there is a tournament of 4 participants: [[1,4],[2,3]] Tournament of 7 participants: [[1,[4,5]],[[2,7],[3,6]]] Or a tournament of 8 participants: [[[1,8],[4,5]],[

Big-O for various Fibonacci Implementations

安稳与你 提交于 2021-02-15 10:26:52
问题 I just tried implementing code (in Java) for various means by which the nth term of the Fibonacci sequence can be computed and I'm hoping to verify what I've learnt. The iterative implementation is as follows: public int iterativeFibonacci(int n) { if ( n == 1 ) return 0; else if ( n == 2 ) return 1; int i = 0, j = 1, sum = 0; for ( ; (n-2) != 0; --n ) { sum = i + j; i = j; j = sum; } return sum; } The recursive implementation is as follows :- public int recursiveFibonacci(int n) { if ( n ==

Big-O for various Fibonacci Implementations

泪湿孤枕 提交于 2021-02-15 10:23:15
问题 I just tried implementing code (in Java) for various means by which the nth term of the Fibonacci sequence can be computed and I'm hoping to verify what I've learnt. The iterative implementation is as follows: public int iterativeFibonacci(int n) { if ( n == 1 ) return 0; else if ( n == 2 ) return 1; int i = 0, j = 1, sum = 0; for ( ; (n-2) != 0; --n ) { sum = i + j; i = j; j = sum; } return sum; } The recursive implementation is as follows :- public int recursiveFibonacci(int n) { if ( n ==

Big-O for various Fibonacci Implementations

三世轮回 提交于 2021-02-15 10:20:54
问题 I just tried implementing code (in Java) for various means by which the nth term of the Fibonacci sequence can be computed and I'm hoping to verify what I've learnt. The iterative implementation is as follows: public int iterativeFibonacci(int n) { if ( n == 1 ) return 0; else if ( n == 2 ) return 1; int i = 0, j = 1, sum = 0; for ( ; (n-2) != 0; --n ) { sum = i + j; i = j; j = sum; } return sum; } The recursive implementation is as follows :- public int recursiveFibonacci(int n) { if ( n ==

Intersection of files

*爱你&永不变心* 提交于 2021-02-13 03:45:29
问题 I two large files (27k lines and 450k lines). They look sort of like: File1: 1 2 A 5 3 2 B 7 6 3 C 8 ... File2: 4 2 C 5 7 2 B 7 6 8 B 8 7 7 F 9 ... I want the lines from both files in which the 3rd column is in both files (note lines with A and F were excluded): OUTPUT: 3 2 B 7 6 3 C 8 4 2 C 5 7 2 B 7 6 8 B 8 whats the best way? 回答1: awk '{print $3}' file1 | sort | uniq > file1col3 awk '{print $3}' file2 | sort | uniq > file2col3 grep -Fx -f file1col3 file2col3 | awk '{print "\\w+ \\w+ " $1 "

Intersection of files

≯℡__Kan透↙ 提交于 2021-02-13 03:43:16
问题 I two large files (27k lines and 450k lines). They look sort of like: File1: 1 2 A 5 3 2 B 7 6 3 C 8 ... File2: 4 2 C 5 7 2 B 7 6 8 B 8 7 7 F 9 ... I want the lines from both files in which the 3rd column is in both files (note lines with A and F were excluded): OUTPUT: 3 2 B 7 6 3 C 8 4 2 C 5 7 2 B 7 6 8 B 8 whats the best way? 回答1: awk '{print $3}' file1 | sort | uniq > file1col3 awk '{print $3}' file2 | sort | uniq > file2col3 grep -Fx -f file1col3 file2col3 | awk '{print "\\w+ \\w+ " $1 "

Knight's Tour in Python - Getting Path from Predecessors

谁都会走 提交于 2021-02-11 18:23:47
问题 I'm trying to adapt a Depth-First Search algorithm in Python to solve the Knight's Tour puzzle. I think I've nearly succeeded, by producing a dictionary of predecessors for all the visited squares. However, I'm stuck on how to find the autual path for the Knight. Currently, using the current return value from tour() in path() gives a disappointing path of [(0, 0), (2, 1)] . I think the crux of the issue is determining at what point inside tour() all squares are visited and at that point

Divide a group of people into teams with constraints

a 夏天 提交于 2021-02-11 18:18:22
问题 So I want to write a small program that would be able to take a group of people (100-200) and divide them into several equal groups (10-15) with constraints. Each person has a city they came from (usually around 8-12 different cities total). Each person was in a group of people before this new division (10-20 different groups). Thats it for the example. Now I want to divide those people in different group such that we strive to have same number of people from different cities in each team (so

Divide a group of people into teams with constraints

半世苍凉 提交于 2021-02-11 18:11:22
问题 So I want to write a small program that would be able to take a group of people (100-200) and divide them into several equal groups (10-15) with constraints. Each person has a city they came from (usually around 8-12 different cities total). Each person was in a group of people before this new division (10-20 different groups). Thats it for the example. Now I want to divide those people in different group such that we strive to have same number of people from different cities in each team (so