Created
October 16, 2014 06:33
-
-
Save sai43/477437cd2c71992fc730 to your computer and use it in GitHub Desktop.
Traingle number puzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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