1

So I am making an application that can solve problems with Empirical Formulae and I need some code that would do something like: If numbers are 2.5, 1, 3 it should change them to 2.5*2 = 5, 1*2 = 2, 3*2 = 6 so that the number with the decimal is converted to a whole number and the other numbers are adjusted appropriately.

I thought of this logic:

for(n = 1; (Math.round(simplestRat[0]) * n) != (int)SimplestRat[0]; n++)

to increment a counter that would multiply an integer to do what I want it to but I am skeptical about this code even at this phase and do not think it will work.

It would be a lot of help if someone could suggest a code for this or improve upon this code or even give me a link to another post for this problem as I was unable to find anything regarding this type of problem.

Any help is appreciated. Thanks

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
namsnath
  • 138
  • 1
  • 3
  • 11
  • 3
    If you are representing your decimals using floating point, you're likely going to experience some rounding errors trying to hit a whole number – Kevin DiTraglia Oct 30 '14 at 17:53
  • Is only one number ever going to be a decimal? Does it have to be the smallest possible multiplier, or just on that works? – FelixMarcus Oct 30 '14 at 17:54
  • 1
    Find the rational representation of each decimal, the number you want to multiply them with is the lcm of the divisors in each fraction. If you want that number to be the smallest possible make sure that your fractions are irreducible. – DSquare Oct 30 '14 at 17:54
  • Another option is to find the number of decimal digits in the number and multiply it with 10 to the power of that (like for 1.25, multiply by 100). After that you can start reducing it to the minimum whole number and multiply other numbers with the factor that comes up – Ankit Bansal Oct 30 '14 at 17:59
  • 1
    You're correct that trying to multiply a floating point number in Java won't work. Like @KevinDiTraglia pointed out, at some point you're going to get something like `1.5 * 2 = 2.9999999...`. You probably need to use something like [BigDecimal](http://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html) which supports arbitrary precision, no matter what algorithm you go with. – azurefrog Oct 30 '14 at 17:59
  • @azurefrog I realized that and was going to use the Math.round function for it but I will look up BigDecimal. Thanks! – namsnath Oct 30 '14 at 18:16
  • @FelixMarcus it is possible that all three may be decimals and yes, they have to be the smallest possible multiplier as empirical formulae are the simplest forms of chemical formulae – namsnath Oct 30 '14 at 18:18

1 Answers1

1

Okay, so you have to have a few steps. First, get them all into whole numbers. The easiest way is to find an appropriate power of ten to multiply them all by that leaves them as integers. This is a useful check: How to test if a double is an integer.

Then cast them to integers, and start working through them looking for common prime factors. This'll be a process similar to Eratosthenes' Sieve (http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) but with division at the end. For each prime, see if all 3 numbers divide by it exactly (modulo prime == 0). If they do, divide and reset the primes to 2. If they don't, next prime.

This should give you the lowest common ratio between the numbers. Any additional multiplier that came from the original stage is shaved off by the common primes method.

Community
  • 1
  • 1
FelixMarcus
  • 380
  • 3
  • 14
  • I Finally got it working after about an hour of running around in circles with my code. Picked up a piece of code from an old program I had made to display prime numbers. Works like a charm. Thanks So Much Felix! – namsnath Oct 30 '14 at 21:18