0

I want to make a method to find a number divisible by a number and add those numbers to an array to be printed out later when I call the method.

I made it to this point:

int [] divider(int n) {
    int [] result = new int[20];
    for (int i = 0; i <= n; i++) {
       if (n%i == 0)
          result = result[i];
    }
    return result;
}

I know it's wrong at many points, but I tried. This is the two thing I know I need to do, but I don't know how:

  1. I know that I have the calculate how many numbers that divisible by a number first to know how big of an array to create. The problem is I know how to find how many number there are, but I don't know how to use that number to create an array by itself in the method.

  2. After that I need to find what is those numbers in which it can divide by another number. This one I could do it, but I don't know how to add these result into an array to print out later.

These are what I know up until now. Please help. Thank you.

Dici
  • 22,025
  • 5
  • 34
  • 74
  • Are you allowed to use ArrayList instead of Array ? – Suresh Atta Oct 07 '15 at 13:58
  • *"I know how to find how many number there are, but I don't know how to use that number to create an array by itself in the method."* Maybe it would be better to read a tutorial instead. Also your question consists of smaller questions that AFAIK have all been asked and answered here. (see http://stackoverflow.com/q/409784/2991525 and http://stackoverflow.com/q/1200621/2991525) – fabian Oct 07 '15 at 14:10
  • @sᴜʀᴇsʜᴀᴛᴛᴀ Yes, I am allowed to. – kaywinpark Oct 07 '15 at 14:25
  • @fabian Thank you so much. – kaywinpark Oct 07 '15 at 14:25

3 Answers3

1

Here is the test:

public static void main(String[] args) {
    Integer [] arr = divider(60);
    for (Integer integer : arr) {
        System.out.println(integer);
    }
}

You should finish your loop with "n/2" because second max divider of "n" must be "n/2": i.e: second max divider of 60 is 30. and "i" must start with "1" because you can not divide a number with "0". After loop finihed we should add "n" into the list because max divider of "n" is "n".

static Integer [] divider(int n) {
    List<Integer> resultList = new ArrayList<Integer>();
    Integer [] result;
    for (int i = 1; i <= n/2; i++) {
       if (n%i == 0)
           resultList.add(i);
    }
    resultList.add(n);
    result = resultList.toArray(new Integer[resultList.size()]);

    return result;
}
Sedat Polat
  • 955
  • 2
  • 10
  • 19
  • Thank you so much for your reply! It works perfectly as I was asking. But can you please explain the "Integer" and what is the last line before the "return" line is? I kinda get it but I'm not sure I did. This line > result = resultList.toArray(new Integer[resultList.size()]); I'm not really sure what it means. – kaywinpark Oct 07 '15 at 14:26
  • @kaywinpark, you can not initialize an array before finding the how many dividers there are. Firstly we should find the dividers and add them into an arraylist. Then, we can create an array which size is "resultList.size()". "resultList.toArray" converts List to array. – Sedat Polat Oct 07 '15 at 14:43
0

you can try to add them to a ArrayList instead, for ArrayList you don't need to know the size before hand, you can just add them as you go along, also for your look you don't have to look at all 1 to n-1 numbers.

if you can't change the definition of your method, then as below you can just add them to a new array. if you can change the definition then you can simply return the result.

int[] divider(int n) {
    ArrayList<Integer> result=new ArrayList<Integer>();
    for (int i = 1; i <= n/2; i++) { //only check for half of the numbers
       if (n%i == 0)
          result = result[i];
    }
    int[] a=new int[result.size()];
    for(int i=0;i<a.length;i++){
       a[i]=results.get(i);
    }
    return a;
}
nafas
  • 5,004
  • 1
  • 23
  • 47
0

This can be done without a List since you can bound the number of numbers that divide your input n. The most trivial way to do it is to say that n cannot have more than n dividers. A more clever bound is sqrt(n). You wanted a size for your array ? Here it is : sqrt(n) + 1 (+1 for a perfect square).

At the end of the method, you can count the actual number of dividers and copy them into a new array. This method may be less efficient than using a List, but if you are limited to arrays this is the way to go.

Dici
  • 22,025
  • 5
  • 34
  • 74
  • u end up with unused allocated memory this way: to be specific you may end up with `sqrt(n) - 2` unused elements – nafas Oct 07 '15 at 14:11
  • I did mention it was less efficient. I just know that in school assignments they often have stupid limitations. It very much looks like a school assignment, so I proposed it while warning about the performance – Dici Oct 07 '15 at 14:18
  • It's actually a practice for myself to do. It's not a school assignment :) But I do set limit for what I can or cannot do as well. – kaywinpark Oct 07 '15 at 14:30