HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Coding multiples that equal 100

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
equal100multiplesthatcoding

Problem

The objective was to see what numbers can be multiplied to equal 100.

Example:

1 x 1 = 1 //no match
5 x 20 = 100 //match


The objective (as I'm learning) was only to use an array as I wanted to practice how it functioned. There are probably other classes I could have used which would have been better, for example, an ArrayList to store a match rather than an array defined of [100].

I'm pretty new to programming and would like your opinion on my following block of code.

public class ArraySearchTest
{

    public ArraySearchTest()
    {
        doTest();
    }

    public void doTest()
    {
        int [] array = new int[100];
        int [] bbb = new int[11];
        int [] ans = new int[100];

        for(int i = 0; i < array.length; i++)
        {
            for(int p = 0; p < bbb.length; p++)
            {
                if( p * i == 100)
                {
                    ans[p] = (p + i);
                    System.out.println(p + " x " + i + " Equals 100");
                }   
            }     
        }
    }
}

Solution

General Style:

Use functions where you can. You have embedded all the code in to one method.

For many reasons, you should create a function which takes the 100 value as a parameter. Additionally, the purpose of the code is to return pairs of numbers that have 100 as the product, so what you want is a container to store each pair.

This is a start to turning your code in to Object Oriented code.

Classes (OOP)

A container for a result, which is two values that multiply to the target, would be called something like CoFactors (Factors which together make the target). It would look something like:

public class CoFactor {
    private final int lesser, greater;

    public CoFactor(int lesser, int greater) {
        this.lesser = lesser;
        this.greater = greater;
    }

    public int getLesser() {
        return lesser;
    }

    public int getGreater() {
        return greater;
    }
}


Algorithm

Now, with that class, we can return the factors of a number with a method like:

public static final List getCoFactors(int target) {
    ....
}


The above method will return a list of factors that have the target as a product.

How would that be coded? First, some math....

  • each factor of a number has a partner (perhaps, if the number is a square number, the partner is itself (3 x 3 = 9 for example) The lesser factor will only be smaller when the lesser factor is less than the square-root of the target....



  • each small factor has a partnering large factor, and there is only one partner.



  • we only need to find the small ones to also be able to find the large ones.



  • we can tell if a number is a factor if there's no remainder after a division (use the % modulo operator)



This can be computed as:

List factors = new ArrayList<>();
int root = (int)Math.sqrt(target);
for (int attempt = 1; attempt <= root; i++) {
    if (target % attempt == 0) {
        factors.add(new CoFactor(attempt, target / attempt));
    }
}
return factors;

Code Snippets

public class CoFactor {
    private final int lesser, greater;

    public CoFactor(int lesser, int greater) {
        this.lesser = lesser;
        this.greater = greater;
    }

    public int getLesser() {
        return lesser;
    }

    public int getGreater() {
        return greater;
    }
}
public static final List<CoFactor> getCoFactors(int target) {
    ....
}
List<CoFactor> factors = new ArrayList<>();
int root = (int)Math.sqrt(target);
for (int attempt = 1; attempt <= root; i++) {
    if (target % attempt == 0) {
        factors.add(new CoFactor(attempt, target / attempt));
    }
}
return factors;

Context

StackExchange Code Review Q#73779, answer score: 8

Revisions (0)

No revisions yet.