পৃষ্ঠাসমূহ

রবিবার, ১১ জুন, ২০১৭

686 - Goldbach's Conjecture (II)

import java.util.*;

public class Main2 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        boolean[] primeList = new boolean[33000];
        primeList[2] = true;
        primeList[3] = true;
        for (int i=4;i<33000;i++) {
            primeList[i] = isPrime(i);
        }
        while (scanner.hasNextInt()) {
            int num = scanner.nextInt();
            if (num == 0) break;
            int counter = 0;
            for (int i=2;i<=num/2;i++) {
                if (primeList[i] && primeList[num-i]) counter++;
            }

            System.out.println(counter);
        }
    }

    static boolean isPrime(int num) {
        for (int i=2;i<=num/2;i++) {
            if (num%i == 0) return false;
        }
        return true;
    }
}

369 - Combinations

import java.math.BigInteger;
import java.util.*;

public class Main2 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextInt()) {
            String line = scanner.nextLine().trim();
            String[] parts = line.split("\\s+");
            String n = parts[0];
            String r = parts[1];
            if (n.equals("0") && r.equals("0")) break;

            BigInteger N = new BigInteger(n);
            BigInteger R = new BigInteger(r);


            BigInteger upper = new BigInteger("1");
            BigInteger lower = new BigInteger("1");
            BigInteger limit = N.subtract(R);

            BigInteger temp = N;
            while (temp.compareTo(limit) == 1) {
                upper = upper.multiply(temp);
                temp = temp.subtract(new BigInteger("1"));
            }

            temp = R;
            while (temp.compareTo(new BigInteger("0")) == 1) {
                lower = lower.multiply(temp);
                temp = temp.subtract(new BigInteger("1"));
            }

            upper = upper.divide(lower);
            System.out.println(n+" things taken "+r + " at a time is "+upper+" exactly.");
        }
    }
}

বৃহস্পতিবার, ৮ জুন, ২০১৭

1210 - Sum of Consecutive Prime Numbers

import java.util.ArrayList;
import java.util.Scanner;

class Another {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> primes = new ArrayList<>();
        primes.add(2);
        primes.add(3);
        int[] values = new int[10001];
        for (int i=4;i<=10000;i++) {
            if (isPrime(i)) primes.add(i);
        }

        for (int i=0;i<primes.size();i++) {
            int total = 0;
            for (int j=i;j<primes.size();j++) {
                total += primes.get(j);
                if (total > 10000) break;
                values[total]++;
            }

        }
        while (scanner.hasNext()) {
            int num = scanner.nextInt();
            if (num == 0) break;
            System.out.println(values[num]);
        }
    }


    static boolean isPrime(int num) {
        if (num == 1 || num == 2 || num == 3) return true;
        for (int i=2;i<=num/2;i++) {
            if (num%i == 0) return false;
        }
        return true;
    }
}