Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Degrees {
- public static ArrayList<Long> triangularNumbers = new ArrayList<>();
- public static boolean isAwesome(double num){
- for(int i=1; i<triangularNumbers.size(); i++){
- for(int j=1; j<triangularNumbers.size(); j++){
- if(triangularNumbers.get(i) + triangularNumbers.get(j) == num && i!=j)
- return true;
- else if(triangularNumbers.get(i) + triangularNumbers.get(j)>num){
- break;
- }
- }
- }
- return false;
- }
- // found on internet, tested extensively, found to be correct
- public static boolean isTriangular(double n){
- long calc_num = (long) (8*n+1);
- long t = (long) Math.sqrt(calc_num);
- if (t*t==calc_num) {
- return true;
- }
- return false;
- }
- public static void main(String[] args) throws IOException {
- Scanner in = new Scanner(Paths.get("C:\\Users\\Gates Wang\\Desktop\\degrees.txt"));
- int count = 0;
- for(int i=0; i<5050813; i++){ //largest number is 5050813, therefore all triangular numbers must be less than 5050813
- if(isTriangular(i)){
- triangularNumbers.add((long)i);
- }
- }
- while(in.hasNext()){
- double num = in.nextDouble();
- if(isAwesome(num))
- count++;
- }
- System.out.println(count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement