Advertisement
apad464

3rd FRQ #1

Apr 11th, 2023 (edited)
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. class Hailstone {
  2.     public static int hailstoneLength(int n) {
  3.         int count = 1;
  4.         while (n != 1) {
  5.             System.out.print(n + " ");
  6.             if (n % 2 == 0) {
  7.                 n /= 2;
  8.             } else if (n % 2 != 0) {
  9.                 n *= 3;
  10.                 n++;
  11.             }
  12.             count++;
  13.         }
  14.         System.out.print("1 ");
  15.         return count;
  16.     }
  17.  
  18.     public static boolean isLongSeq(int n) {
  19.         boolean isLong = hailstoneLength(n) > n;
  20.         System.out.println(isLong);
  21.         return isLong;
  22.     }
  23.  
  24.     public static double propLong(int n) {
  25.         double count = 0;
  26.         for (int i = 1; i <= n; i++) {
  27.             if (isLongSeq(i)) count++;
  28.         }
  29.         return count / n;
  30.     }
  31. }
  32.  
  33. public class HailstoneRunner {
  34.     public static void main(String[] args) {
  35.         System.out.println(Hailstone.isLongSeq(5));
  36.         System.out.println(Hailstone.isLongSeq(8));
  37.  
  38.         System.out.println("\nProportion section");
  39.         System.out.println(Hailstone.propLong(10));
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement