| New file |
| | |
| | | public class BandMatrix { |
| | | public static void main(String[] args) { |
| | | int n = Integer.parseInt(args[0]); |
| | | int distanza = Integer.parseInt(args[1]); |
| | | |
| | | for (int i = 0; i < n; i++) { |
| | | for (int j = 0; j < n; j++) { |
| | | if (Math.abs(i - j) <= distanza) { |
| | | System.out.print("* "); |
| | | } else { |
| | | System.out.print("0 "); |
| | | } |
| | | } |
| | | System.out.println(); |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | public class GeneralizedHarmonic { |
| | | public static void main(String[] args) { |
| | | int n = Integer.parseInt(args[0]); |
| | | int m = Integer.parseInt(args[1]); |
| | | double sum = 0.0; |
| | | |
| | | for (int i = 1; i <= n; i++) { |
| | | sum += 1.0 / Math.pow(i, m); |
| | | } |
| | | |
| | | System.out.println(sum); |
| | | } |
| | | } |
| New file |
| | |
| | | public class RandomWalker { |
| | | public static void main(String[] args) { |
| | | int x = 0; |
| | | int y = 0; |
| | | int passi = 0; |
| | | int limite = Integer.parseInt(args[0]); |
| | | |
| | | System.out.println("(" + x + "," + y + ")"); |
| | | while (Math.abs(x) + Math.abs(y) < limite) { |
| | | double r = Math.random(); |
| | | if (r < 0.25) { |
| | | x++; |
| | | } else if (r < 0.5) { |
| | | x--; |
| | | } else if (r < 0.75) { |
| | | y++; |
| | | } else { |
| | | y--; |
| | | } |
| | | passi++; |
| | | System.out.println("(" + x + "," + y + ")"); |
| | | } |
| | | System.out.println("steps = " + passi); |
| | | } |
| | | } |
| New file |
| | |
| | | public class RandomWalkers { |
| | | public static void main(String[] args) { |
| | | |
| | | int limite = Integer.parseInt(args[0]); |
| | | int cicli = Integer.parseInt(args[1]); |
| | | int totalePassi = 0; |
| | | for (int i = 0; i < cicli; i++) { |
| | | |
| | | int x = 0; |
| | | int y = 0; |
| | | int passi = 0; |
| | | |
| | | while (Math.abs(x) + Math.abs(y) < limite) { |
| | | double r = Math.random(); |
| | | if (r < 0.25) { |
| | | x++; |
| | | } else if (r < 0.5) { |
| | | x--; |
| | | } else if (r < 0.75) { |
| | | y++; |
| | | } else { |
| | | y--; |
| | | } |
| | | passi++; |
| | | } |
| | | |
| | | totalePassi += passi; |
| | | } |
| | | |
| | | System.out.println("average number of steps = " + ((double) totalePassi / cicli)); |
| | | } |
| | | |
| | | } |