Sequence of non-squares: Difference between revisions

→‎{{header|Python}}: Python 2 -> Python 3
(added Ol)
(→‎{{header|Python}}: Python 2 -> Python 3)
Line 1,883:
 
=={{header|Python}}==
<lang python>>>> from math import floor, sqrt
>>> def non_square(n):
>>> # (Using the fact that round(X) is equivalent to floor(0.5+X) for our range of X)
>>> def nonsqr(n): return n + int(roundfloor(1/2 + sqrt(n)))
 
>>> # first 22 values (as a list) has no squares:
>>> print(*map(non_square, range(1, 23)))
[2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27]
 
>>> # first 22 values (as a list) has no squares:
>>> [nonsqr(i) for i in xrange(1,23)]
[2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27]
>>> # The following check shows no squares up to one million:
>>> fordef i in xrangeis_square(1,1000000n):
j = return sqrt(nonsqr(in).is_integer()
 
assert j != int(j), "Found a square in the sequence: %i" % i
>>> non_squares = map(non_square, range(1, 10 ** 6))
>>> next(filter(is_square, non_squares))
StopIteration Traceback (most recent call last)
<ipython-input-45-f32645fc1c0a> in <module>()
1 non_squares = map(non_square, range(1, 10 ** 6))
----> 2 next(filter(is_square, non_squares))
 
StopIteration: </lang>
>>></lang>
 
=={{header|R}}==
35

edits