Need some help with an equation(s) for a basic Java program

In summary, the programmer is trying to optimize a function which breaks down any number input into smaller values with a maximum usage. He is struggling with the maths and needs help from others.
  • #1
Billogi
5
0
I am writing a basic program using Java. The function is to break up any number the user inputs into smaller values, the catch is that these smaller values have a maximum usage and the overall result has to use the combination with the highest amount of values. I am figuring out the code just fine. I have made a class for the values to define their value, max value and max usage, it is the maths I am struggling with.

For example, the input is 377. I need to break it down into a *maximum* of twenty 1's, twenty 2's and one hundred 5's but my desired result has to be the most long winded, so in this case 17 1s, 10 2s and 68 5's. So I need to be able to figure out the maximum amount of 1s to allow the maximum amount of 2s to ,in turn, allow the maximum amount of 5s. And this needs to work with any number the user inputs.

I would later add higher numbers with set maximum usage and a finally the value of 100 with infinite useage. For now I just need a few equations to work with.

Can anyone shed a little light on a few possible ways of doing this? thanks =)
 
Last edited:
Technology news on Phys.org
  • #2
Can you explain further the process of breaking down a number? For the example input of 377, you cite at least 20 1's, 20 2's and 100 5's, but the sum of these is 560. I am not sure then what you mean by breaking a number down. How does the resulting breakdown come about?
 
  • #3
Well, with my example it would need to be broken down into 17 1s, 10 2s and 68 5's as that would give you the maximum number of individual values used (rather than say 1 1, 3 2s and 74 5s).

Sorry, I did say at least, I meant a maximum of! I've been on it for hours now, think it is time for a rest ha
 
  • #4
I'm afraid I am still unclear about what you are doing.

What is to stop you from exchanging one of the 5's for five 1's?
 
  • #5
MarkFL - there can be at most 20 1's, 20 2's and 100 5's, that's why you can't add an extra five 1's. That said, you can add another 10 2's by sacrificing four 5's, or even an extra three 1's and one extra 2 by removing one 5, so I still don't understand what is being optimized either.

Did you mean a maximum of twenty 1's, ten 2's and a hundred 5's?​
 
  • #6
Yes, I did forget the constraint on the 1's and 2's. :D
 
  • #7
It seems to me if N is the input, an algorithm (pseudo-code) which might work is:

Code:
let ones be the number of 1's
let twos be the number of 2's
let fives be the number of 5's

get input N

if N > 20 then

     ones = 20

else

     ones = N

end if

N = N - ones

if N > 10 then

     twos = 10

     elseif N > 0 then

          twos = N/2

     else

          end program

end if

N = N - 2*twos

if N > 0 then

     let R = mod(N,5)

     if R = 0 then

          fives = N/5

     else

          if R = 1 then

               twos = twos - 2
               N = N + 4

          elseif R = 2

               twos = twos - 1
               ones = ones - 1
               N = N + 3

          elseif R = 3

               twos = twos - 1
               N = N + 2

          elseif R = 4

               ones = ones - 1
               N = N + 1

          end if

          fives = N/5

     end if

end if

N = N - 5*fives

if N != 0 then

     ERROR

else

     end program

end if

Note: I have not tested this code, but see if this works for you. If N > 540, then you will get fives > 100.

I have edited it for the constraint that there be ten 2's.
 
Last edited:
  • #8
I have moved this topic to this sub-forum since it does not really fit with the usual topics posted in the Pre-Algebra and Algebra sub-forum. I feel this sub-forum is the best fit because of the interdisciplinary description.

We try to keep the topics here organized in the most useful manner for our members, but please do not feel that you have done anything wrong, as topics like this can be tricky to decide where best to post. :D
 
  • #9
Bacterius said:

Did you mean a maximum of twenty 1's, ten 2's and a hundred 5's?​

Ah, yes I did! It's amazing what spending over four hours trying to solve a problem can do to your thought process =P

Well, my thought process at least

And Mark, thank you for the help with the pseudo-code. I will give it a try tonight :)
 
  • #10
You will have to amend the code I wrote a bit then for the constraint that there be a maximum of ten 2's. :D

edit: I made the change already.
 

FAQ: Need some help with an equation(s) for a basic Java program

What is a basic Java program?

A basic Java program is a set of instructions written in the Java programming language that can be executed by a computer. It typically includes variables, data types, control structures, and functions, and is used to perform specific tasks or solve problems.

How do I write an equation in Java?

To write an equation in Java, you can use the mathematical operators such as +, -, *, and /, along with variables and constants. For example, to calculate the area of a rectangle, you can use the equation "area = length * width" in your Java program.

How can I get help with equations in Java?

If you need help with equations in Java, there are many online resources such as tutorials, forums, and documentation available. You can also consult with a Java expert or join a coding community to get guidance and support.

What is the importance of equations in Java programming?

Equations are important in Java programming as they allow you to perform calculations and manipulate data, which are essential for creating useful and functional programs. They also help in problem-solving and can make your code more efficient and concise.

Can you provide an example of an equation in a Java program?

Yes, here is an example of a simple equation in Java that calculates the area of a circle using the radius:

double area = Math.PI * Math.pow(radius, 2);

This equation uses the built-in Math class and its methods to perform the calculation and assign the result to the variable "area".

Similar threads

Replies
4
Views
2K
Replies
3
Views
1K
Replies
4
Views
2K
Replies
15
Views
2K
Replies
2
Views
3K
Replies
7
Views
2K
Replies
3
Views
2K
Back
Top