algorithm

Most efficient way to sum up an array of integers

爱⌒轻易说出口 提交于 2021-02-19 03:38:45
问题 I am trying to find a sub- O(n) method to calculate the sum of an integer array ~~~(instead of iterating through 0 - n , I am doing it in n/2 )~~~ I'm still doing it in O(n) . public static int sum(int[] s) { int length = s.length - 1; int half = length/2; int sum = 0; for(int i = 0; i <= half; i++) { System.out.println(i + " " + s[i] + " + " + s[length - i]); sum += s[i] + s[length - i]; } return sum; } My algorithm works for even number of integers, however, when there are odd number of

Numerically calculate combinations of factorials and polynomials

一个人想着一个人 提交于 2021-02-19 03:36:29
问题 I am trying to write a short C++ routine to calculate the following function F(i,j,z) for given integers j > i (typically they lie between 0 and 100) and complex number z (bounded by |z| < 100), where L are the associated Laguerre Polynomials: The issue is that I want this function to be callable from within a CUDA kernel (i.e. with a __device__ attribute). Standard library/Boost/etc functions are therefore out of the questions, unless they are simple enough to re-implement on my own - this

How can I improve this square root method?

£可爱£侵袭症+ 提交于 2021-02-19 02:09:25
问题 I know this sounds like a homework assignment, but it isn't. Lately I've been interested in algorithms used to perform certain mathematical operations, such as sine, square root, etc. At the moment, I'm trying to write the Babylonian method of computing square roots in C#. So far, I have this: public static double SquareRoot(double x) { if (x == 0) return 0; double r = x / 2; // this is inefficient, but I can't find a better way // to get a close estimate for the starting value of r double

Set Popping (Python)

别来无恙 提交于 2021-02-18 21:06:51
问题 Lets say you have a set: foo = {1, 2, 3, 4, 5} In the book I am currently reading, Pro Python, it says that using foo.pop() will pop an arbitrary number from that selection. BUT...When I try it out, it pops 1, then 2, then 3... Does it do it arbitrarily, or is this just a coincidence? 回答1: The reason it says it is arbitrary is because there is no guarantee about the ordering it will pop out. Since you just created the set, it may be storing the elements in a "nice" order, and thus .pop()

Set Popping (Python)

∥☆過路亽.° 提交于 2021-02-18 21:06:09
问题 Lets say you have a set: foo = {1, 2, 3, 4, 5} In the book I am currently reading, Pro Python, it says that using foo.pop() will pop an arbitrary number from that selection. BUT...When I try it out, it pops 1, then 2, then 3... Does it do it arbitrarily, or is this just a coincidence? 回答1: The reason it says it is arbitrary is because there is no guarantee about the ordering it will pop out. Since you just created the set, it may be storing the elements in a "nice" order, and thus .pop()

On complexity of recursive descent parsers

浪子不回头ぞ 提交于 2021-02-18 20:32:58
问题 It's known that recursive descent parsers may require exponential time in some cases; could anyone point me to the samples, where this happens? Especially interested in cases for PEG (i.e. with prioritized choices). 回答1: It's because you can end up parsing the same things (check the same rule at the same position) many times in different recursion branches. It's kind of like calculating the n-th Fibonacci number using recursion. Grammar: A -> xA | xB | x B -> yA | xA | y | A S -> A Input:

How do you pin point the location of a user with 3 nodes, using Triangulation?

泄露秘密 提交于 2021-02-18 19:34:25
问题 I am trying to find a user through their Bluetooth strength (RSSI value). I have 3 Raspberry PIs, each gathering the signal strength of the user. Lets say the nodes returned: node1 = 65 node2 = 70 node3 = 75 How would I find the user through triangulation and pin point them on a map, and output the RSSI value? I have researched Trilateration and Ceva's Theorem but do not know how to implement them. I am unsure on how to locate the nodes in an environment, do I give the main node a location of

How do you pin point the location of a user with 3 nodes, using Triangulation?

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-18 19:34:05
问题 I am trying to find a user through their Bluetooth strength (RSSI value). I have 3 Raspberry PIs, each gathering the signal strength of the user. Lets say the nodes returned: node1 = 65 node2 = 70 node3 = 75 How would I find the user through triangulation and pin point them on a map, and output the RSSI value? I have researched Trilateration and Ceva's Theorem but do not know how to implement them. I am unsure on how to locate the nodes in an environment, do I give the main node a location of

How do I find largest valid sequence of parentheses and brackets in a string?

笑着哭i 提交于 2021-02-18 12:47:15
问题 So I have a script I need to write and one of the largest problems boils down to finding the largest valid subsequence within a string. So I have something like "()(({}[](][{[()]}]{})))(" as an input and I would need to return "[{[()]}]{}" as an output. I have tried using a stack like structure like you would do if it was just parentheses but haven't been able to figure out something that works. I'd prefer a solution in python but any guidance anyone can offer will help regardless of language

Finding components of very large graph

岁酱吖の 提交于 2021-02-18 12:00:12
问题 I have a very large graph represented in a text file of size about 1TB with each edge as follows. From-node to-node I would like to split it into its weakly connected components. If it was smaller I could load it into networkx and run their component finding algorithms. For example http://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.components.connected.connected_components.html#networkx.algorithms.components.connected.connected_components Is there any way