Jump to content

Square but not cube: Difference between revisions

Added Rust and Swift solutions
(Add False)
(Added Rust and Swift solutions)
Line 1,982:
Square-and-cubes:
1 64 729</pre>
 
=={{header|Rust}}==
<lang rust>fn main() {
let mut s = 1;
let mut c = 1;
let mut cube = 1;
let mut n = 0;
while n < 30 {
let square = s * s;
while cube < square {
c += 1;
cube = c * c * c;
}
if cube == square {
println!("{} is a square and a cube.", square);
} else {
println!("{}", square);
n += 1;
}
s += 1;
}
}</lang>
 
{{out}}
<pre>
1 is a square and a cube.
4
9
16
25
36
49
64 is a square and a cube.
81
100
121
144
169
196
225
256
289
324
361
400
441
484
529
576
625
676
729 is a square and a cube.
784
841
900
961
1024
1089
</pre>
 
=={{header|Scala}}==
Line 2,020 ⟶ 2,079:
First 15 positive integers that are both a square and a cube:
1 64 729 4096 15625 46656 117649 262144 531441 1000000 1771561 2985984 4826809 7529536 11390625
</pre>
 
=={{header|Swift}}==
<lang swift>var s = 1, c = 1, cube = 1, n = 0
while n < 30 {
let square = s * s
while cube < square {
c += 1
cube = c * c * c
}
if cube == square {
print("\(square) is a square and a cube.")
} else {
print(square)
n += 1
}
s += 1
}</lang>
 
{{out}}
<pre>
1 is a square and a cube.
4
9
16
25
36
49
64 is a square and a cube.
81
100
121
144
169
196
225
256
289
324
361
400
441
484
529
576
625
676
729 is a square and a cube.
784
841
900
961
1024
1089
</pre>
 
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.