পৃষ্ঠাসমূহ

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

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;
    }
}

শনিবার, ৬ মে, ২০১৭

uva 113

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uva100;

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
       
        Scanner scanner = new Scanner(System.in);
       
        while (scanner.hasNextDouble()) {
            double n = scanner.nextDouble();
       
            double p = scanner.nextDouble();
       
            System.out.printf("%.0f\n",Math.pow(p, (1/n)));
        }
       
     
       
    }
}