For , , and integer , all terms are nonnegative, so squaring preserves the inequalities:
This gives integer-square membership in a square-root Beatty sequence without evaluating a floating-point square root. Since , any witnessing satisfies .
Scan candidate output integers in increasing order while maintaining the smallest possible witness:
spectrum(r,n):
    k = 1
    for i = 1,...,n-1:
        while r*k*k < i*i:
            k = k+1
        if r*k*k < (i+1)*(i+1):
            Out.Int(i)
The inner loop supplies the left inequality from part (a), and the final test checks the strict right inequality. This prints each qualifying integer once. The witness index never decreases and is at most after the inner loop, so its total number of increments is at most . Consequently the whole nonrecursive program uses integer arithmetic operations, not iterations of a fresh search. Exact products or overflow-safe comparisons are required; arbitrary-precision bit costs depend on operand sizes.
Before testing candidate , every earlier integer has been printed exactly when it belongs to the spectrum, and every positive satisfies . During the inner loop that exclusion remains true: an excluded candidate is passed over only after checking its square.
At termination of the inner loop, , so is the least positive possible witness. If its square is already at least , every larger witness is too large as well; otherwise it satisfies both required inequalities and is printed. Advancing preserves the earlier-witness exclusion because the new lower bound only increases. These loop invariants establish both completeness and absence of extra outputs.
Use binary search for the least positive integer whose square reaches the lower bound:
test(i,r):
    lo = 1; hi = i
    while lo < hi:
        mid = lo + (hi-lo)//2
        if r*mid*mid < i*i:
            lo = mid+1
        else:
            hi = mid
    return r*lo*lo < (i+1)*(i+1)
The search interval is valid because . Once the least witness is located, the final strict upper test decides membership, by part (a). No recursion or approximate square root is used.
Maintain that all positive fail , while hi satisfies that inequality; its least satisfying integer lies in . Monotonicity of in positive validates both updates. At exit the endpoints agree and identify exactly that least integer.
The loop variant hi-lo decreases on every iteration and is at least halved up to rounding. Starting with at most candidates therefore needs arithmetic operations, conventionally for . For the loop is empty. The final upper-bound check proves the returned Boolean is the required membership answer.

Articles by others on the same topic (0)

There are currently no matching articles.