How to assume a number to be a half integer?

For integer x $ \left(\cos 2\pi x\right) ^2=1$ and I can check this as follows

Simplify[Cos[2 \[Pi] x]^2, Assumptions -> x \[Element] Integers] 

and get 1 as an output. The same holds when x is half-integer. How do I assume this in Mathematica? More generally, is there a nice way to implement assumptions of the type say $ x\in 3\mathbb{Z}+2$ without redefining variables that I use?

Evaluate the limit of a subsequence: Compute the limit $ \lim_{n\to\infty} \cos\left( \pi \sqrt{4n^2 + 5n + 1} \right) $ Integer $n$ on Mathematica

I want to compute the limit $ $ \lim \limits_{n\to\infty} \cos\left( \pi \sqrt{4n^2 + 5n + 1} \right) $ $ for integer $ n$ . By completing the square, we can determine that this limit is equal to $ – \tfrac1{\sqrt2} \approx -0.7071 $ .

But if we don’t restrict $ n$ to an integer, then the limit is indeterminate / does not exist. And can be easily found by typing it on WolframAlpha. Or in Mathematica:

However, I do not know how to compute the limit (on Mathematica) with the original constraint that $ n$ must be an integer.

I know that we can plot a graph on Mathematica:

But what is the exact value of the limit?

The graph suggests that the limit is equal to $ -\tfrac1{\sqrt2} $ . However, this doesn’t look like a convincing result because we can’t know that the limit is exactly equal to $ -\tfrac1{\sqrt2} $ .

Question: Is there a way to compute this limit in Mathematica where it spits out a single numerical value (of $ -1/{\sqrt2}$ )?

Determine if there is a subset of the given set with sum divisible by a given integer

I’ve been given a question to solve:

Given a set of non-negative distinct integers, and a value $ m$ , determine if there is a subset of the given set with sum divisible by $ m$ .

For this question the answer is here

I don’t understand the part after if DP[j]==True
what is actually the intuition behind this code. Please explain in detail.

Enumerating points on the integer lattice, within a sphere, sorted by angle, in O(1) space

Inspired by this StackOverflow question:

(it was not clearly presented, and got closed)

Let’s say I wanted to enumerate all the 3D points on the integer lattice, within a sphere, in order of the angle between the vector to the point and the up vector (say z).

Could I do this in O(1) space efficiently?

All I can find is:

Remembering last point (init at (0,0,0)). (O(1) memory) while true     init best dot product to 0     going through all 3D points (three nested for's with radius range)         if this point has better dot product but least than last point             keep this point as best     if best dot product is still 0, exit      pick best point as current point (this is where the listing occurs)     update last point to best point 

Not only is this absolutely slow, it also needs the use of integer math for dot product and length so that numerical precision doesn’t mess with symmetrical points and it would also need a tweak to guarantee symmetrical points are listed in a known order.

Is there any good algorithms that would apply here?

Integer Palindrome

I wrote the following program to calculate integer palindrome.

class Solution: # @param A : integer # @return an integer def isPalindrome(self, A):     if A<0:         return 0     return int(str(A) == reversed(str(A))) 

It’s giving the wrong output could anyone pls tell what’s going wrong.

What factors of the integer dataset being sorted can I change, in order to compare two sorting algorithms?

I am comparing two comparison and binary data structure based sorting algorithms, the Tree Sort, and the Heap Sort. I am measuring the time taken for both algorithms to sort an increasing size of an integer dataset. However, I am wondering if there are any other variables which I can modify, for example standard deviation, in the integer dataset itself that would be of any benefit to my comparison.

Returning random integer from interval based on last result and a seed

Suppose we have an interval of integers [a, b]. I would like to have a function that returns random members from within the interval, without repetitions. Once that all members within the interval are explored, the function would start to return the same first random sequence again, in the same order.

Example: a=1, b=5

3, 1, 4, 5, 2, 3, 1, 4, 5, 2, 3, 1, 4, 5, 2, ... 

This would be easy to achieve by shuffling an array of all elements between a and b, and repeating it once the array is finished. However, this would take too much memory space, and this is not suitable for my case (I might have millions of elements).

Instead, the function I’d like to have would be more or less like this:

f(a, b, n, seed) -> n+1 


a - start of interval b - end of interval n - last element returned from list seed - self-explanatory n+1 - next random element from list, calculated by using the seed and the last element returned (n) 

The trick is knowing some way to get a non-repeated number from the interval based only on the element returned before and the seed. In the end, it would behave like a circular list randomized at its initialization, but without using memory space.

Complexity of Integer Factorization

In Quantum Information and Quantum Computation by Nielsen and Chuang, they define the complexity class NP as follows (page 142):

A language $ L$ is in NP if there is a turing machine $ M$ with the following properties.

  1. If $ x\in L$ then there exists a witness string $ w$ such that $ M$ halts in the state $ q_Y$ ("yes state") after a time polynomial in $ |x|$ when the machine is started in the state $ x$ -blank-$ w$ .
  2. If $ x \not \in L$ then for all strings $ w$ which attempt to play the role of a witness, the machine halts in state $ q_N$ ("no state") after a time polynomial in $ |x|$ when $ M$ is started in the state $ x$ -blank-$ w$ .

This definition is motivated by the factoring decision problem, where they identify "witness strings" $ w$ with possible factors of $ x$ .

My confusion is, based on how NP is defined, it seems like we are able to construct a polynomial time algorithm for solving the factoring decision problem. For a given string $ x$ , start the factoring turing machine $ M$ in the state $ x$ -blank-$ w$ for all $ w < x$ , and check whether the machine ever halts in $ q_Y$ . Since there are $ O(|x|)$ witnesses to check, and for each witness, the machine will halt in polynomial time, it follows that this algorithm will determine whether $ x$ has factors in polynomial time.

Clearly this shouldn’t work, but I am unsure where the flaw in my logic is.

Is deciding solvability of systems of quadratic equations with integer coefficients over the reals in NP?

In the book ‘Computational Complexity’ by Arora and Barak the following question is posed (exercise 2.20.):

Let REALQUADEQ be the language of all satisfiable sets of quadratic equations over real variables. Show that REALQUADEQ is NP-complete.

I know how to show NP-hardness, but I’m stuck when it comes to proving that this problem is in NP, in particular how to show that we can describe a solution using a polynomial number of bits.

I did some research and found out that over the complex numbers, it remains an open question if the problem is in NP [1]. It also seems closely related to the existential theory of the reals, which again is not known to be in NP.

Thus my question: Is this problem known to be in NP? And if so, could somebody point me in the right direction regarding the proof.

[1] Pascal Koiran, Hilbert’s Nullstellensatz is in the Polynomial Hierarchy, Journal of Complexity 12 (1996), no. 4, pp. 273–286.