c++,algorithm,math,discrete-mathematics

This is Dirichlet's divisor summatory function D(x). Using the following formula (source) where gives the following O(sqrt(n)) psuedo-code (that happens to be valid Python): def seq_sum(n): sqrtn = int(math.sqrt(n)) return sum(n // k for k in range(1, sqrtn + 1)) * 2 - sqrtn ** 2 Notes: The // operator...

algorithm,discrete-mathematics

The loops in fact only go up to the cube root of N. (i^3 < n, etc.) The 3 nested loops of this length, give O(cube root of N, cubed). This O(N) Of note, if you were correct and they each went to one third of N, then cubing this...

math,statistics,discrete-mathematics,moving-average,usage-statistics

The classical solution to this kind of problem is using the median instead of the average. You can also combine these two ideas: discard a certain amount of values (e.g. the upper and the lower 25%) and compute the mean of the remainder. This is called a truncated mean.

algorithm,permutation,discrete-mathematics,lexicographic

You can do it this way: // n is the number of elements in the given sequence p = all permutations of [0, 1, ..., n - 1] in lexicographical order for permutation in p: for i = 0 ... n - 1 print(s[permutation[i]]) // s is the given sequence...

c++,algorithm,math,data-structures,discrete-mathematics

I don't know about the first statement, but the second statement can be said false by the following argument: Since there are sqrt(n) sequences, with n elements each, the total number of elements in n*sqrt(n). In the worst case, you would need to check every element at least once to...

algorithm,math,combinations,permutation,discrete-mathematics

Inversions The key to this type of problem (of swapping adjacent elements) is to consider the number of inversions in the permutation after each swap. Two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j. The number of inversions in a permutation is the...

python,algorithm,math,sequence,discrete-mathematics

There are many possible solutions to this problem. Here's a reusable solution that simply decrements from 4 to 1 and adds the expected number of 1's and 0's. Loops used : 1 def sequence(n): string = "" for i in range(n): string+='1'*(n-i) string+='0'*(n-i) return string print sequence(4) There's another single-line...

matlab,math,permutation,discrete-mathematics,elementary-functions

Yes, this seems to be a bug. Good catch! But probably a bug in the documentation, rather than in the function. If you type open perms to see the source code, you'll see the following description in the first lines: %PERMS All possible permutations. % PERMS(1:N), or PERMS(V) where V...

graph,discrete-mathematics,graph-coloring

in here it is so clear,by the way the sentence is In an optimal coloring there must be at least one of the graph’s m edges between every pair of color classes I'll break it down and make every part more clear. In an optimal coloring : this means that...

javascript,html5,canvas,geometry,discrete-mathematics

Here's one way: Save all your line segments (walls) in an array. var walls=[]; var wall={x0:50,y0:50,x1:150,y1:150}; walls.push(wall); When you are dragging your window into place, compare the mouse position to the nearest point on every line segment (wall). Place the window on whichever wall is closest to the mouse. This...

math,sequence,discrete-mathematics,cyclic

Another way to approach this problem. Note that F(n) = F(n - 1) - F(n - 2) is the same as F(n) - F(n - 1) + F(n - 2) = 0 which makes it a linear difference equation. Such equations have fundamental solutions a^n where a is a root...

f#,mapping,intervals,discrete-mathematics

let interval = 10 let index = [6;12;18;24] let value =[101;102;103;104] let intervals = List.map (fun e -> e/interval) index let keys = List.map2(fun e1 e2 -> (e1,e2)) intervals value let skeys = Seq.ofList keys let result = skeys |>Seq.groupBy (fun p -> fst p) |>Seq.map (fun p -> snd...

string,algorithm,probability,combinatorics,discrete-mathematics

Suppose you build up palindrome-free strings one letter at a time. For the first letter, you have M choices, and for the second, you have M-1, since you can't use the first letter. This much is obvious. For every letter after the first two, you can't use the previous letter,...

math,sequence,discrete-mathematics,cartesian

(On edit: I added a second function which enables the Cartesian coordinate version to be obtained directly.) I got this far before my head exploded. It is closed form in the sense that it gives the coordinates of say the one millionth square without needing to place them one by...

discrete-mathematics,solver,simulated-annealing,rostering,tabu-search

If you go with OptaPlanner and don't want to follow the Employee Rostering design of assigning 8 hours Shifts (planning entities) to Employees (planning value), because of your 2th constraint, then you could try to follow the Cheap Time Example design, something like this: @PlanningEntity public class WorkAssignment { Employee...

java,algorithm,discrete-mathematics

You should decompose your problem. Global algorithm IMO, you should start at n = 0, and go up. For each value of n, check if the sequence of digits needed to represent it is allowed. As soon as you can't, n is your answer. Smaller part The new problem is...

php,user-input,discrete-mathematics

Try this <?php $n = $_POST['c1']; $p = trim(str_replace(' ', '', $_POST['c2'])); // Remove spaces eval("\$result = {$p};"); echo $result; ?> ...

algorithm,math,discrete-mathematics,nearest-neighbor

I would advice you to go for Locality-sensitive hashing (LSH), which is in trend right now. It reduces the dimensionality of high-dimensional data, but I am not sure if your dimension will go well with that algorithm. See the Wikipedia page for more. You can use your own metric, but...

algorithm,big-o,discrete-mathematics,proof

Your proof is 100% correct. An alternative one is using limits to infinity. lim_(x->infinity) (x+1)!/x! when x->infinity = lim_(x->infinity) x = infinity The above contradicts (x+1)! for being in O(x!)...

algorithm,graph,graph-theory,discrete-mathematics,minimum-spanning-tree

Yes, the algorithm always finds a spanning tree (if such exist, i.e. the graph has a source). Not sure if it finds a minimal spanning tree though. You have already proven there are no cycles in the resulting graph, so we only need to show connectivity. Proof that the resulting...

database,algorithm,math,sharding,discrete-mathematics

One data structure suitable for this application is a Bloom filter. A Bloom filter is a probabilistic data structure which allows you test whether an item is already in a set. If the test returning false then the item is definitely not in the set (0% false negatives), if true...

algorithm,graph-algorithm,discrete-mathematics,proof

Disclaimer: I don't know how much formal you want your proof to be and I'm not familiar with formal proofs. induction on the while loop: Is it true at the beginning? Does it remain true after a step (quite simple path property)? same idea, induction on k (why k+1???): Is...

algorithm,math,graph,discrete-mathematics

Let V be the number of vertices, E be the number of edges, F3 be the number of triangles, and F4 be the number of quads. We have equations V = 16 # given 4 V = 2 E # number of vertex-edge incidences 3 F3 + 4 F4 =...