How to resolve the algorithm Square but not cube step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Square but not cube step by step in the D programming language
Table of Contents
Problem Statement
Show the first 30 positive integers which are squares but not cubes of such integers. Optionally, show also the first 3 positive integers which are both squares and cubes, and mark them as such.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Square but not cube step by step in the D programming language
Source code in the d programming language
import std.algorithm;
import std.range;
import std.stdio;
auto squareGen() {
struct Gen {
private int add = 3;
private int curr = 1;
bool empty() {
return curr < 0;
}
auto front() {
return curr;
}
void popFront() {
curr += add;
add += 2;
}
}
return Gen();
}
auto cubeGen() {
struct Gen {
private int add1 = 7;
private int add2 = 12;
private int curr = 1;
bool empty() {
return curr < 0;
}
auto front() {
return curr;
}
void popFront() {
curr += add1;
add1 += add2;
add2 += 6;
}
}
return Gen();
}
auto merge() {
struct Gen {
private auto sg = squareGen();
private auto cg = cubeGen();
bool empty() {
return sg.empty || cg.empty;
}
auto front() {
import std.typecons;
if (sg.front == cg.front) {
return tuple!("num", "isCube")(sg.front, true);
} else {
return tuple!("num", "isCube")(sg.front, false);
}
}
void popFront() {
while (true) {
if (sg.front < cg.front) {
sg.popFront();
return;
} else if (sg.front == cg.front) {
sg.popFront();
cg.popFront();
return;
} else {
cg.popFront();
}
}
}
}
return Gen();
}
void main() {
foreach (p; merge.take(33)) {
if (p.isCube) {
writeln(p.num, " (also cube)");
} else {
writeln(p.num);
}
}
}
You may also check:How to resolve the algorithm Filter step by step in the Slate programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the TI-83 BASIC programming language
You may also check:How to resolve the algorithm Smarandache-Wellin primes step by step in the Wren programming language
You may also check:How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the ZX Spectrum Basic programming language