Sequence of non-squares: Difference between revisions

Add ABC
No edit summary
(Add ABC)
 
(9 intermediate revisions by 7 users not shown)
Line 34:
No squares found
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN non.square n:
RETURN n + floor (1/2 + root n)
HOW TO REPORT square n:
REPORT n = (floor root n)**2
 
FOR n IN {1..22}: WRITE non.square n
WRITE /
 
IF NO n IN {1..1000000} HAS square non.square n:
WRITE "No squares occur for n < 1.000.000"</syntaxhighlight>
{{out}}
<pre>2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
No squares occur for n < 1.000.000</pre>
 
=={{header|Ada}}==
Line 399 ⟶ 415:
The functions int, round, floor, ceil are taken from [http://www.pixelbeat.org/scripts/bc here] (int is slightly modified) ([http://www.pixelbeat.org/scripts/ Here] he states the license is GPL).
 
=={{header|BurlesqueBQN}}==
<syntaxhighlight lang="bqn"> NonSquare ← +⟜(⌊0.5+√)
IsSquare ← =⟜⌊√
 
NonSquare 1+↕22
⟨ 2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27 ⟩
+´ IsSquare NonSquare 1+↕1e6
0</syntaxhighlight>
 
=={{header|Burlesque}}==
<syntaxhighlight lang="burlesque">
1 22r@{?s0.5?+av?+}[m
Line 499 ⟶ 523:
</pre>
 
Alternatively, without using an external library
=={{header|Clojure}}==
<syntaxhighlight lang="cpp">
#include <cmath>
#include <cstdint>
#include <iostream>
 
uint32_t non_square(const uint32_t& n) {
return n + static_cast<uint32_t>(0.5 + sqrt(n));
}
 
int main() {
std::cout << "The first 22 non-square numbers:" << std::endl;
for ( uint32_t i = 1; i <= 22; ++i ) {
std::cout << non_square(i) << " ";
}
std::cout << std::endl << std::endl;
 
uint32_t count = 0;
for ( uint32_t i = 1; i < 1'000'000; ++i ) {
double square_root = sqrt(non_square(i));
if ( square_root == floor(square_root) ) {
count++;
}
}
std::cout << "Number of squares less than 1'000'000 produced by the formula: " << count << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>The first 22 non-square numbers:
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
 
Number of squares less than 1'000'000 produced by the formula: 0</pre>
 
=={{header|Chipmunk Basic}}==
{{trans|BASIC256}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="basic">10 rem Sequence of non-squares
20 cls
30 ' Display first 22 values
40 for i = 1 to 22
50 print nonsqr(i) " ";
60 next i
70 print
80 ' Check for squares up to one million
90 found = 0
100 for i = 1 to 1000000
110 j = sqr(nonsqr(i))
120 if j = int(j) then
130 found = 1
140 print "Found square: " i
150 exit for
160 endif
170 next i
180 if found = 0 then print "No squares occur for n < 1000000"
190 end
200 sub nonsqr(n)
210 nonsqr = n+int(0.5+sqr(n))
220 return</syntaxhighlight>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">;; provides floor and sqrt, but we use Java's sqrt as it's faster
;; (Clojure's is more exact)
Line 741 ⟶ 823:
{{out}}
<pre>2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func nonSqu n .
return n + floor (0.5 + sqrt n)
.
for i = 1 to 22
print nonSqu i
.
for i = 1 to 1e6
j = sqrt nonSqu i
if j = floor j
found = 1
.
.
if found = 0
print "No squares found"
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,732 ⟶ 1,833:
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
No square numbers were generated for n <= 1000000</syntaxhighlight>
 
No loops
<syntaxhighlight lang="matlab">
sum(ismember((1:1:sqrt(1e6-1)).^2,(1:1e6-1) + floor(1/2 + sqrt((1:1e6-1)))))
</syntaxhighlight>
 
=={{header|Maxima}}==
Line 2,836 ⟶ 2,942:
end for;
end func;</syntaxhighlight>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program sequence_of_non_squares;
print([nonsquare n : n in [1..22]]);
 
if exists n in [1..1000000] | is_square nonsquare n then
print("Found square", nonsquare n, "at", n);
else
print("No squares found up to 1 million");
end if;
 
op is_square(n);
return (floor sqrt n)**2 = n;
end op;
 
op nonsquare(n);
return n + floor(0.5 + sqrt n);
end op;
end program;</syntaxhighlight>
{{out}}
<pre>[2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27]
No squares found up to 1 million</pre>
 
=={{header|Sidef}}==
Line 3,105 ⟶ 3,233:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
System.print("The first 22 numbers in the sequence are:")
2,096

edits