[SOLVED] How can I print out n numbers that satisfy the condition

Issue

This Content is from Stack Overflow. Question asked by Nutnicha

I have written a code that would return either 1 or 4 as the output, and here it is

public static int findSum(int k) {
            int sum = 0;
            boolean foundNonZero = false;
            for (; k != 0; k /= 10) {
                if (foundNonZero) {
                    sum += (k % 10)*(k % 10);
                } else {
                    foundNonZero = k % 10 != 0;
                }
            }
            return sum;
        }

Now, I’m trying to write a function public static long[] firstK(int k) that would return k numbers starting from 0 that would satisfy the condition that findsum = 1.

I’m struggling to understand how to do it, despite reading Java syntax and information. and here is my code so far:

public static long[] firstK(int k) {
    while (int x = 0;) {
        if (findSum(x) = 1;)
            System.out.println(x);
        } 

I know that int(k) isn’t used in this, but I have no idea how to implement it. Any help would be greatly appreciated 🙂



Solution

Maybe something like this:

public static List<Integer> firstK(int k) {
    List<Integer> result = new ArrayList<>();
    for (int x = 0 ; result.size() < k ; x++)
        if (findSum(x) == 1)
            result.add(k);
    return result;
}

If you really want an array rather than a List, I would still use a List to compute the result. I would then create an array from that list and return that as the function’s result to have the function return a int[] or long[]. I can’t see why you’d want to return long[] or List<Long> rather than int[] or List<Integer>, I doubt the code involved would ever be given the chance to iterate past the maximum integer value.

UPDATE: Here’s a complete example that uses an array throughout:

public class Test {

    public static int findSum(int k) {
        int sum = 0;
        boolean foundNonZero = false;
        for (; k != 0; k /= 10) {
            if (foundNonZero) {
                sum += (k % 10)*(k % 10);
            } else {
                foundNonZero = k % 10 != 0;
            }
        }
        return sum;
    }

    public static long[] firstK(int k) {
        long[] result = new long[k];
        int i = 0;
        for (int x = 0; i < k; x++)
            if (findSum(x) == 1)
                result[i++] = x;
        return result;
    }

    public static void main(String[] args) {
        long[] r = firstK(5);
        System.out.println(Arrays.toString(r));
    }
}

Result:

[11, 12, 13, 14, 15]

The use of an array during the operation is fine because you do know the size of that array up front. You often don’t, in which case it is usually cleaner to use a List to collect terms, and then convert to an array at the end. I did it this way initially out of habit. In practice, at least in my case, I pretty much never use arrays due to them being less flexible. They ARE more compact and can perform better, but it is rarely the case that the difference matters.


This Question was asked in StackOverflow by Nutnicha and Answered by CryptoFool It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?