Problem (inspired by Project Euler 29):
Consider all integer combinations of ab for X ≤ a ≤ Y and X ≤ b ≤ Y
Return a sequence of distinct terms in numerical order
Input is a list of 2 integers separated by a space (
" "
), representing the lower and upper bounds (X Y). Input is read from txt file.Output is a numerically sorted list of all unique numbers generated by the function (ab) , separated by space. Output is written to txt file.
Example:
Input: 2 5
Output: 4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125
And the code:
Option Explicit On
Option Strict On
Option Infer On
Option Compare Text
Imports System
Imports System.IO
Imports System.Windows.Forms
Module ProjectEuler29
Sub Main()
Dim myBounds = GetInput("C:\temp\square.txt")
Dim myResults = UseBoundsToCreateList(myBounds)
WriteToFile("C:\temp\square-answer.txt", myResults)
End Sub
Private Function GetInput(ByVal inputPath As String) As Integer()
Dim returnText As String = File.ReadAllText(inputPath)
Dim returnArray As String() = returnText.Split(New String() {}, StringSplitOptions.None)
Return ConvertStringToIntegerArray(returnArray)
End Function
Private Function ConvertStringToIntegerArray(ByVal arrayOfStrings As String()) As Integer()
Return Array.ConvertAll(arrayOfStrings, Function(str) Int32.Parse(str))
End Function
Private Function UseBoundsToCreateList(ByVal myBounds As Integer()) As String
Dim numberOfIntegers = myBounds(myBounds.Length - 1) - myBounds(0) + 1
Dim resultList As New List(Of Double)
Dim i As Integer
Dim myResult As Double
For i = myBounds(0) To myBounds(0) + numberOfIntegers - 1
For j = myBounds(0) To myBounds(0) + numberOfIntegers - 1
myResult = i ^ j
If Not resultList.Contains(myResult) Then resultList.Add(myResult)
Next
Next
resultList.Sort()
Return ChangeListDblToString(resultList)
End Function
Private Function ChangeListDblToString(ByVal resultList As List(Of Double)) As String
Return String.Join(" ", resultList.ToArray)
End Function
Private Sub WriteToFile(ByVal outputPath As String, ByVal returnData As String)
Dim fileAuthor As New StreamWriter(outputPath)
If Not File.Exists(outputPath) Then File.CreateText(outputPath)
fileAuthor.WriteLine(returnData)
fileAuthor.Close()
End Sub
End Module