SvetlanPetrova

Tribonacci Sequence SoftUni

Jun 25th, 2021
930
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class TribonacciSequence {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int count = Integer.parseInt(scanner.nextLine());
  7.         int[] numbersToPrint = numbersToPrint(count);
  8.         for (int i = 0; i < count; i++) {
  9.             System.out.print(numbersToPrint[i] + " ");
  10.         }
  11.  
  12.     }
  13.     private static int[] numbersToPrint(int count) {
  14.         int[] numbersArray = new int[count];
  15.         for (int i = 0; i < count; i++) {
  16.             if (i == 0 || i == 1) {
  17.                 numbersArray[i] = 1;
  18.             }
  19.             else if (i == 2) {
  20.                 numbersArray[i] = 2;
  21.             }
  22.             else if (i > 1) {
  23.                 numbersArray[i] = numbersArray[i - 1] + numbersArray[i - 2] + numbersArray[i-3];
  24.             }
  25.         }
  26.         return numbersArray;
  27.     }
  28. }
  29.  
  30.  
RAW Paste Data