Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Coding: Triangle Numbers Puzzle - from http://www.gild.com/
package pack;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TriangleNumbersPuzzle
{
private static int returnNumberOfDivisors(int inNum)
{
int numDivisors = 0;
for (int index = inNum; index > 0; index--)
if ((inNum % index) == 0) numDivisors++;
return numDivisors;
}
private static void recursiveTriangleNumberOverInputValue(int[] inputValues)
{
if (returnNumberOfDivisors(returnTriangleNumberOfInput(++inputValues[0])) >= (inputValues[1] + 1))
System.out.println(returnTriangleNumberOfInput(inputValues[0]));
else recursiveTriangleNumberOverInputValue(inputValues);
}
private static int readInFile(String filePath)
{
int inChar, inputNumber = 0;
StringBuilder strBuilder = new StringBuilder();
try {
FileInputStream fileIns = new FileInputStream(new File(filePath));
while ((inChar = fileIns.read()) != -1)
strBuilder.append((char) inChar);
fileIns.close();
inputNumber = Integer.parseInt(strBuilder.toString().trim());
} catch (FileNotFoundException fnfe) {
} catch (IOException ioe) {
} catch (NumberFormatException nfe) { }
return inputNumber;
}
private static int returnTriangleNumberOfInput(int inValue)
{
return inValue*(inValue+1)/2;
}
public static void main(String[] args)
{
int[] inputValues = {readInFile(args[0]),readInFile(args[0])};
recursiveTriangleNumberOverInputValue(inputValues);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.