index

2020 A - Question 1 - powerful number

A powerful number is a positive integer m such that for every prime number p dividing m, p2 also divides m

The following is a list of all powerful numbers between 1 and 200:

1, 4, 8, 9, 16, 25, 27, 32, 36, 49, 64, 72, 81, 100, 108, 121, 125, 128, 144, 169, 196, 200

Solution

public class test2020A_Q1 {
    //check if prime number
    public static boolean isPrime(int n) {
        for (int i = 2; i <= Math.sqrt(n); i++)
            if (n % i == 0)
                return false;

        return true;
    }

    //get array of all prime numbers that divide n
    public static int[] prime_dividers(int n) {
        ArrayList<Integer> listOfPrimes = new ArrayList();

        // prime must be greater than 1
        if (n <= 1)
            return new int[0];

        if (n <= 2)
            return new int[]{2};

        for (int i = 2; i <= n; i++)
            if (isPrime(i) && n%i==0)
                listOfPrimes.add(i);

        int[] primes = new int[listOfPrimes.size()];
        for (int i = 0; i < primes.length; i++)
            primes[i] = listOfPrimes.get(i);

        return primes;
    }

    public static boolean powerful(int n) {
        int[] primes_to_check = prime_dividers(n);

        if (n == 1)
            return true;

        if (primes_to_check.length==0)
            return false;

        for (int i = 0; i < primes_to_check.length; i++)
                if(n%primes_to_check[i]!=0 || n % Math.pow(primes_to_check[i], 2)!=0 )
                    return false;

        return true;
    }



    public static void main(String[] args) {
        for (int i = -10; i <= 100; i++) {
            if (powerful(i))
                System.out.println(i + " is powerful number");
        }
    }
}

2020 A - Question 2

Solution

Arrays

2020 A - Question 3

Solution

2020 A - Question 4 - Ellipse

Solution

2020 B - Question 2 - Strings : Find Max number

2020 B - Question 3 - arrays : diagonal

2020 B - Question 5 - linked lists : Add to an ordered Linked List

img

We did this question

Test Example 3: Question 2

Test Exercise: Add to an ordered Linked List

Test Example 2: Question 1

longest string

Solution

Test Example 2: Question 3

we did this already

Test Example 2: Question 5

simplest approach, first sort then check

Last updated

Was this helpful?