Casting out nines: Difference between revisions
Content added Content deleted
Line 323: | Line 323: | ||
1 16 17 32 33 48 49 64 65 80 81 96 97 112 113 128 129 144 145 160 161 176 177 192 193 208 209 224 225 240 241 256 257 272 273 288 |
1 16 17 32 33 48 49 64 65 80 81 96 97 112 113 128 129 144 145 160 161 176 177 192 193 208 209 224 225 240 241 256 257 272 273 288 |
||
</pre> |
</pre> |
||
=={{header|C#}}== |
|||
{{trans|Java}} |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
namespace CastingOutNines { |
|||
public static class Helper { |
|||
public static string AsString<T>(this IEnumerable<T> e) { |
|||
var it = e.GetEnumerator(); |
|||
StringBuilder builder = new StringBuilder(); |
|||
builder.Append("["); |
|||
if (it.MoveNext()) { |
|||
builder.Append(it.Current); |
|||
} |
|||
while (it.MoveNext()) { |
|||
builder.Append(", "); |
|||
builder.Append(it.Current); |
|||
} |
|||
builder.Append("]"); |
|||
return builder.ToString(); |
|||
} |
|||
} |
|||
class Program { |
|||
static List<int> CastOut(int @base, int start, int end) { |
|||
int[] ran = Enumerable |
|||
.Range(0, @base - 1) |
|||
.Where(a => a % (@base - 1) == (a * a) % (@base - 1)) |
|||
.ToArray(); |
|||
int x = start / (@base - 1); |
|||
List<int> result = new List<int>(); |
|||
while (true) { |
|||
foreach (int n in ran) { |
|||
int k = (@base - 1) * x + n; |
|||
if (k < start) { |
|||
continue; |
|||
} |
|||
if (k > end) { |
|||
return result; |
|||
} |
|||
result.Add(k); |
|||
} |
|||
x++; |
|||
} |
|||
} |
|||
static void Main() { |
|||
Console.WriteLine(CastOut(16, 1, 255).AsString()); |
|||
Console.WriteLine(CastOut(10, 1, 99).AsString()); |
|||
Console.WriteLine(CastOut(17, 1, 288).AsString()); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>[1, 6, 10, 15, 16, 21, 25, 30, 31, 36, 40, 45, 46, 51, 55, 60, 61, 66, 70, 75, 76, 81, 85, 90, 91, 96, 100, 105, 106, 111, 115, 120, 121, 126, 130, 135, 136, 141, 145, 150, 151, 156, 160, 165, 166, 171, 175, 180, 181, 186, 190, 195, 196, 201, 205, 210, 211, 216, 220, 225, 226, 231, 235, 240, 241, 246, 250, 255] |
|||
[1, 9, 10, 18, 19, 27, 28, 36, 37, 45, 46, 54, 55, 63, 64, 72, 73, 81, 82, 90, 91, 99] |
|||
[1, 16, 17, 32, 33, 48, 49, 64, 65, 80, 81, 96, 97, 112, 113, 128, 129, 144, 145, 160, 161, 176, 177, 192, 193, 208, 209, 224, 225, 240, 241, 256, 257, 272, 273, 288]</pre> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |