Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Square-but-not-cube/C-sharp/square-but-not-cube.cs
Normal file
41
Task/Square-but-not-cube/C-sharp/square-but-not-cube.cs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using static System.Console;
|
||||
using static System.Linq.Enumerable;
|
||||
|
||||
public static class SquareButNotCube
|
||||
{
|
||||
public static void Main() {
|
||||
var squares = from i in Integers() select i * i;
|
||||
var cubes = from i in Integers() select i * i * i;
|
||||
|
||||
foreach (var x in Merge().Take(33)) {
|
||||
WriteLine(x.isCube ? x.n + " (also cube)" : x.n + "");
|
||||
}
|
||||
|
||||
IEnumerable<int> Integers() {
|
||||
for (int i = 1; ;i++) yield return i;
|
||||
}
|
||||
|
||||
IEnumerable<(int n, bool isCube)> Merge() {
|
||||
using (var s = squares.GetEnumerator())
|
||||
using (var c = cubes.GetEnumerator()) {
|
||||
s.MoveNext();
|
||||
c.MoveNext();
|
||||
while (true) {
|
||||
if (s.Current < c.Current) {
|
||||
yield return (s.Current, false);
|
||||
s.MoveNext();
|
||||
} else if (s.Current == c.Current) {
|
||||
yield return (s.Current, true);
|
||||
s.MoveNext();
|
||||
c.MoveNext();
|
||||
} else {
|
||||
c.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue