TheBulgarianWolf

Tribonacci number and spiral - number match

Mar 25th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. public class Main
  4. {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         int first = Integer.parseInt(sc.nextLine());
  8.         int second = Integer.parseInt(sc.nextLine());
  9.         int third = Integer.parseInt(sc.nextLine());
  10.         int spiralCurrent = Integer.parseInt(sc.nextLine());
  11.         int spiralIncrease = Integer.parseInt(sc.nextLine());
  12.         ArrayList<Integer> tribonacciNumbers = new ArrayList<>();
  13.         tribonacciNumbers.add(first);
  14.         tribonacciNumbers.add(second);
  15.         tribonacciNumbers.add(third);
  16.        
  17.         int tribonacciCurrent = third;
  18.        
  19.         while(tribonacciCurrent < 1000000){
  20.             tribonacciCurrent = first + second + third;
  21.             tribonacciNumbers.add(tribonacciCurrent);
  22.            
  23.             first = second;
  24.             second = third;
  25.             third = tribonacciCurrent;
  26.         }
  27.        
  28.         ArrayList<Integer> spiralNumbers = new ArrayList<Integer>();
  29.         spiralNumbers.add(spiralCurrent);
  30.         int spiralCount = 0;
  31.         int spiralStepMul = 1;
  32.        
  33.         while(spiralCurrent < 1000000){
  34.             spiralCurrent += spiralIncrease * spiralStepMul;
  35.             spiralNumbers.add(spiralCurrent);
  36.             spiralCount++;
  37.             if(spiralCount % 2 == 0){
  38.                 spiralStepMul++;
  39.             }
  40.         }
  41.        
  42.        
  43.         boolean found = false;
  44.         for(int i =0;i<tribonacciNumbers.size();i++){
  45.             for(int j = 0;j<spiralNumbers.size();j++){
  46.                 if(tribonacciNumbers.get(i).equals(spiralNumbers.get(j)) && tribonacciNumbers.get(i) < 10000000){
  47.                     System.out.println("Match at number: " + tribonacciNumbers.get(i));
  48.                     found = true;
  49.                     break;
  50.                 }
  51.                
  52.                
  53.             }
  54.            
  55.             if(found){
  56.                     break;
  57.                 }
  58.         }
  59.        
  60.         if(!found){
  61.             System.out.println("NO!");
  62.         }
  63.        
  64.     }
  65. }
Add Comment
Please, Sign In to add comment