'Sorted Array of n square (n*n)

Input is n (any integer)

Output should be a orted array of n square (n*n) , n , Closest Prime Number to n , Closest Fibonacci number to n

How can I get the output?



Solution 1:[1]

Well this does seem to be pretty easy. First you have to calculate all these numbers.

n square is pretty straightforward so I'm not going to explain it.

For the prime number you start by checking n then you check n-1 and n+1 and so on until you find it.

For the Fibonacci number you have to calculate the series (which has for formula F[i+1]=F[i]+F[i-1]) until you go over n, then take the number closer to n.

Once you got everything you need to sort 4 numbers which is something I assume you can do.

Solution 2:[2]

Solution

 public static int[] sortedSquaredArray(int[] array,int  n) {
    
            int[] result = new int[array.length];
            for (int i = 0; i < array.length; i++) {
                int sum=1;
                for(int j=0;j<n;j++) {
                   sum *= array[i] ;
                }
                result[i]=sum;
            }
            Arrays.sort(result);
            return result;
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 meneldal
Solution 2 Govind Sharma