Advertisement
arabdy

Numero impar *3+1 y par /2

Sep 30th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. public class clase {
  2.  
  3.     public static void main(String[] args) {
  4.         numeros(//numero Cualquiera!
  5.     }
  6.  
  7.     /**
  8.      *
  9.      * @param k
  10.      */
  11.     public static void numeros(long k) {
  12.  
  13.         System.out.print(k + ", ");
  14.  
  15.         while (k != 1) { // mientras k sea diferente de 1:
  16.  
  17.             // debo preguntar si k es par o impar
  18.             if (k % 2 == 0) { // k es par:
  19.                 // k se debe dividir en 2
  20.                 k = k / 2;
  21.             } else { // k es impar:
  22.                 // k se debe multiplicar por 3 y sumar 1
  23.                 k = k * 3 + 1;
  24.             }
  25.  
  26.             System.out.print(k + ",");
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement