e5a1eb3a012293ddb7e7110a084855d94106d9a3..0e284a3b0e031f2c6d59b12112df1a6c3bc5b289
2025-10-05 Cristiano Magro
Esame seconda lezione
0e284a diff | tree
2025-10-04 Cristiano Magro
altri esercizi
851e6f diff | tree
7 files added
132 ■■■■■ changed files
src/main/java/BandMatrix.java 17 ●●●●● patch | view | raw | blame | history
src/main/java/DivisorPattern.java 16 ●●●●● patch | view | raw | blame | history
src/main/java/GeneralizedHarmonic.java 13 ●●●●● patch | view | raw | blame | history
src/main/java/PowerOfTwo.java 15 ●●●●● patch | view | raw | blame | history
src/main/java/RandomWalker.java 25 ●●●●● patch | view | raw | blame | history
src/main/java/RandomWalkers.java 33 ●●●●● patch | view | raw | blame | history
src/main/java/TenHello.java 13 ●●●●● patch | view | raw | blame | history
src/main/java/BandMatrix.java
New file
@@ -0,0 +1,17 @@
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();
        }
    }
}
src/main/java/DivisorPattern.java
New file
@@ -0,0 +1,16 @@
public class DivisorPattern {
    public static void main(String[] args) {
        int limite = Integer.parseInt(args[0]);
        for (int i = 1; i <= limite; i++) {
            for (int j = 1; j <= i; j++) {
                if (i % j == 0) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}
src/main/java/GeneralizedHarmonic.java
New file
@@ -0,0 +1,13 @@
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);
    }
}
src/main/java/PowerOfTwo.java
New file
@@ -0,0 +1,15 @@
public class PowerOfTwo {
    public static void main(String[] args) {
        int limite = Integer.parseInt(args[0]);
        int conta = 1;
        for (int i = 1; i <= limite; i++) {
            int potenza = i * i;
            if (potenza <= limite) {
                System.out.println(conta + " " + potenza);
                conta++;
            }
        }
    }
}
src/main/java/RandomWalker.java
New file
@@ -0,0 +1,25 @@
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);
    }
}
src/main/java/RandomWalkers.java
New file
@@ -0,0 +1,33 @@
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));
    }
}
src/main/java/TenHello.java
New file
@@ -0,0 +1,13 @@
public class TenHello {
    public static void main(String[] args) {
        System.out.println("Hello, World! " + "1st");
        System.out.println("Hello, World! " + "2sd");
        System.out.println("Hello, World! " + "3rd");
        for (int i = 4; i <= 10; i++) {
            System.out.println("Hello, World! " + i + "th");
        }
    }
}