Bored in progging right now, I wrote an algorithm that uses the modulus operator. This algorithm is much more efficient and will work at the same speed regardless of input number. The only thing that slows this one down is a long array of potential factors, since it needs to run all possible combinations of the factor array. It uses two methods, but only one is recursive so you could probably combine them.import java.util.Scanner;
public class FactoringAlgorithm {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] a = {6,9,20};
while (true) {
System.out.print("Number: ");
int n = in.nextInt();
if (n == 0) break;
System.out.println(checkFactoring(n, a));
}
}
private static boolean checkFactoring(int target, int[] factors) {
for (int i = 1; i <= factors.length; i++) {
if (checkRegression(target, factors, i) == true) return true;
}
return false;
}
private static boolean checkRegression(int target, int[] factors, int len) {
if (factors.length <= 1) {
if (target % factors[0] == 0) return true;
else return false;
}
for (int i = 1; i < factors.length; i++) {
if (i + len - 1 > factors.length) return false;
int factor = factors[0];
int j = i;
for (int z = 1; z < len; z++) {
factor += factors[j];
j++;
}
if (target % factor == 0) return true;
}
// Shorten the array
if (factors.length > 1) {
int[] n = new int[factors.length - 1];
for (int i = 1; i < factors.length; i++) {
n[i-1] = factors[i];
}
if (checkRegression(target,n,len) == true) return true;
}
return false;
}
}
None.
is this a contest now?
maybe i'll work on my code ;o
None.
compete all you like, im okay with the original code that DTBK provided, I am modifying it so that it displays what I want it to display and I am trying to find a solution that doesn't use arrays because apparently im not supposed to use an array...
None.
wtf? no arrays?
who gives programming homework with boundaries?
None.
ha. eat it ip. my solution doesn't use arrays. it uses stacked booleans. i win.
None.
we were taught arrays in grade 11 this is grade 12 class, I had the grade 11 class last semester (to catch up) and now im in the grade 12 class, the teacher wasn't present yesterday but the students said that we arn't allowed to use arrays because apparently he hasn't re-taught arrays to us yet, however I beg to differ because on the first three days we did review and there were array questions, so I don't think that no arrays is actually a requirement, it doesn't say it on the rubric so I think that they should be okay, he can't exactly tell me that I don't know arrays, because I clearly know arrays... at most he can take maybe 1 - 2 marks away but most of the marks are in presentation, documentation, file location etc...
None.
I talked to him and we are ^-^
None.
Quote from DT_Battlekruser
If you flatly aren't allowed to use arrays, you can be really clever and write the equivalent of the ArrayList class yourself 
Technically, if he can't use arrays, he could use the ArrayList class...
None.
Hey DTBK I am gonna post up my reformated version of your code in a while, do you think you could check if my comments are correct, I believe them to be correct but I could be mistaken.
None.
/**
* McFuPucks is a java class that decides whether the user's input amount of pucks is obtainable via boxes of 6,9 and/or 20
*
* Rowan Collins
* Version 1.0
*/
import java.util.Scanner;
public class McFuPucks
{ //start of McFuPucks class
public static void main(String[] args)
{ //start of main method
/**
* Main acquires the users desired pucks and initiates the recurring method "buyable"
*/
Scanner input = new Scanner(System.in); //input is the scanner object used to obtain user input
System.out.print("Input the number of McFuPucks you'd like to purchase: ");
int i = input.nextInt(); //i is the desired amount of McFuPucks
int[] boxSizes = {6,9,20}; //boxSizes is the different variety of box sizes
if(buyable(i, boxSizes) == true)
{
System.out.println("This is buyable");
}
else
{
System.out.println("This is not buyable");
}
} //end of main method
private static boolean buyable(int x, int[] array)
{ //start of buyable method
/**
* buyable is a method that reccurs in order to find if the desired amount of pucks is obtainable using a variety of boxsizes
*
* @param int x is the desired amount of McFuPucks
* @param int[] array is the different boxsizes
*
* @return true or false depending on whether or not it is purchasable
*/
int n = 0; //n is the temporary value used in comparison against x (target value) to decide if n is too low, too high or 'buyable'
while (true)
{ //start of recursive loop
if (n == x)
{
return true;
}
if (array.length > 1)
{
int[] shortArray = new int[array.length - 1];
for (int i = 1; i < array.length; i++)
{
shortArray[i - 1] = array[i];
}
if (buyable(x - n, shortArray))
{
return true;
}
}
n += array[0];
if (n > x)
{
return false;
}
} //end of recursive loop
} //end of buyable method
} //end of McFuPucks class
None.
What the hell is
if(buyable(i, boxSizes) == true)
?
I think you meant
if(buyable(i, boxSizes))
My point: be more elegant! Well, at least DTBK used what I suggested (in a much uglier way). -__-
Also, explain your algorithm in your comments. It's not whether or not you can understand it, it's whether or not someone who doesn't want to look at the code can understand it in two minutes or less. .. Unless you have a really long algorithm.
None.
I needed it so that instead of saying true or false it said "This is buyable" or "This is not buyable" so I had to do true vs false, unless what you're saying outputs the same result just without the == true
I just need to make sure that the comments I do have make sense and are correct so that my teacher wont be like "wtf iz dis nuub talking about"
None.
if(buyable()) and if(buyable() == TRUE) are the exact same thing... as long as buyable only returns true or false.
Like in my PHP code, I could have put if (count($valid) == TRUE) (i do believe all non-zeros are "TRUE" anyways ;o) or if (count($valid) > 0)... but it was unnecesary ;o
None.
did I get n right though?
None.
Sometimes the type thing in php annoys me.
Shocko, wtf comments.
{ //start of McFuPucks class
is that really necessary? ;o I need to do something about tab conversion inside code blocks...
None.
Test your own code. Does it work?
I don't have a java compiler on this computer because this is teh gaming station but test your code before handing it in.
None.