Generator/Exponential: Difference between revisions
Content added Content deleted
(I'm so sorry I misunderstood test condition. I verified this new code against all the integer solution (i,j) of i^3 = j^2 and confident that this is accurate.) |
(→{{header|Visual Basic .NET}}: Added VB.NET) |
||
Line 3,416: | Line 3,416: | ||
End Sub</lang>{{out}} |
End Sub</lang>{{out}} |
||
<pre> 529 576 625 676 784 841 900 961 1024 1089 </pre> |
<pre> 529 576 625 676 784 841 900 961 1024 1089 </pre> |
||
=={{header|Visual Basic .NET}}== |
|||
<lang vbnet>Module Program |
|||
Iterator Function IntegerPowers(exp As Integer) As IEnumerable(Of Integer) |
|||
Dim i As Integer = 0 |
|||
Do |
|||
Yield CInt(Math.Pow(i, exp)) |
|||
i += 1 |
|||
Loop |
|||
End Function |
|||
Function Squares() As IEnumerable(Of Integer) |
|||
Return IntegerPowers(2) |
|||
End Function |
|||
Function Cubes() As IEnumerable(Of Integer) |
|||
Return IntegerPowers(3) |
|||
End Function |
|||
Iterator Function SquaresWithoutCubes() As IEnumerable(Of Integer) |
|||
Dim cubeSequence = Cubes().GetEnumerator() |
|||
Dim nextGreaterOrEqualCube As Integer = 0 |
|||
For Each curSquare In Squares() |
|||
Do While nextGreaterOrEqualCube < curSquare |
|||
cubeSequence.MoveNext() |
|||
nextGreaterOrEqualCube = cubeSequence.Current |
|||
Loop |
|||
If nextGreaterOrEqualCube <> curSquare Then Yield curSquare |
|||
Next |
|||
End Function |
|||
Sub Main() |
|||
For Each x In From i In SquaresWithoutCubes() Skip 20 Take 10 |
|||
Console.WriteLine(x) |
|||
Next |
|||
End Sub |
|||
End Module</lang> |
|||
More concise but slower implementation that relies on LINQ-to-objects to achieve generator behavior (runs slower due to re-enumerating Cubes() for every element of Squares()). |
|||
<lang vbnet> Function SquaresWithoutCubesLinq() As IEnumerable(Of Integer) |
|||
Return Squares().Where(Function(s) s <> Cubes().First(Function(c) c >= s)) |
|||
End Function</lang> |
|||
{{out}} |
|||
<pre>529 |
|||
576 |
|||
625 |
|||
676 |
|||
784 |
|||
841 |
|||
900 |
|||
961 |
|||
1024 |
|||
1089</pre> |
|||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
<lang XPL0>code ChOut=8, IntOut=11; |
<lang XPL0>code ChOut=8, IntOut=11; |