## Weekend Programming Challenge – Week #60 solutions

We got 15 solutions this weekend

• Python x6
• Java x2
• C x2
• C++ x1
• Perl x1
• C# x1
• JavaScript x1

The solutions are uploaded at Github

1. Sorry for bad formatting of Solution 10

public class KTY81_110 {
public static double temperature(double r) {
// 5 multiplications and 5 sums to convert
return r * (r * (r * (r * (1.588560974e-14 * r – 1.077078151e-10) + 2.969300166e-7) – 4.399145663e-4) + 4.672892621e-1) – 207.5343491;
}

public static void main(String[] args) {
double[] rt = {
490, 515, 567, 624, 684, 747, 815, 886, 961, 1000, 1040, 1122, 1209,
1299, 1392, 1490, 1591, 1696, 1805, 1915, 1970, 2023, 2124, 2211
};
for (double r : rt) {
System.out.println(“resistence = ” + r + ” –> ” + temperature(r) + ” °C”);
}
}

/**
* Aproximate PTC KTY81-110 resistence/temperaure table (table 7 of data sheet)
*
* 490, -55
* 515, -50
* 567, -40
* 624, -30
* 684, -20
* 747, -10
* 815, 0
* 886, 10
* 961, 20
* 1000, 25
* 1040, 30
* 1122, 40
* 1209, 50
* 1299, 60
* 1392, 70
* 1490, 80
* 1591, 90
* 1696, 100
* 1805, 110
* 1915, 120
* 1970, 125
* 2023, 130
* 2124, 140
* 2211, 150
*
* with 5th degree polynomial.
* Coefficients are calculated with least square fitting algorithm. You can do that using Maxima or other mathematical tools. For
* this example I used the online tool available at http://www.xuru.org/rt/PR.asp
* The maximum error is about 0.12 °C With e 4th degree polynomial maximum error is about 0.6 °C
* Only one line of code: 5 multiplications and 5 sums for converting Ohm to °C
*/
/**
* The output would be:
*
* resistence = 490.0 –> -55.012990561937244 °C
* resistence = 515.0 –> -49.999927124126856 °C
* resistence = 567.0 –> -40.084587938252355 °C
* resistence = 624.0 –> -29.919840603614347 °C
* resistence = 684.0 –> -19.90123226735483 °C
* resistence = 747.0 –> -10.017892333864182 °C
* resistence = 815.0 –> 0.03715975304885433 °C
* resistence = 886.0 –> 9.970996751391993 °C
* resistence = 961.0 –> 19.944039263748095 °C
* resistence = 1000.0 –> 24.94815793999996 °C
* resistence = 1040.0 –> 29.965138489594807 °C
* resistence = 1122.0 –> 39.91947276591071 °C
* resistence = 1209.0 –> 50.04434619330664 °C
* resistence = 1299.0 –> 60.08989753013145 °C
* resistence = 1392.0 –> 70.04354849181851 °C
* resistence = 1490.0 –> 80.09021667667372 °C
* resistence = 1591.0 –> 90.00722463854464 °C
* resistence = 1696.0 –> 99.9170450489961 °C
* resistence = 1805.0 –> 109.90949337509704 °C
* resistence = 1915.0 –> 119.92443855386045 °C
* resistence = 1970.0 –> 125.01372245527227 °C
* resistence = 2023.0 –> 130.04071647290408 °C
* resistence = 2124.0 –> 140.18985180904372 °C
* resistence = 2211.0 –> 149.8809989468441 °C
*
*/
}

• Nikolay Dimitrov
Jul 08, 2014 @ 11:16:15

Thanks mate. I really appreciate that you added your code. And yes, this is what I hoped to see, excellent work! Looks like I’m not the only one who didn’t like the error of the 4th order polynomial :).

Regards.

2. Nikolay Dimitrov
Jul 08, 2014 @ 04:59:06

Solution 15 (Mark Butowski, C) doesn’t have the source code uploaded in Olimex GitHub, its instead in another repository, and such repositories have the tendency to dissapear at some point, which wouldn’t be fair for the other WPC participants.

@Mark – the solution is cool, good work.

3. Nikolay Dimitrov
Jul 08, 2014 @ 05:47:06

Solution 10 (Carlo Amaglio, Java) – The source code is missing, although Carlo clearly had a good idea how to solve the problem. So unless we qualify English as a programming language, I think this is not a solution.

• Sorry, the code is there (Java) but bad formatting…
I leave the first comment with a more readable code. The necessary code is only one line with 5 multiplications and 5 sums.
best regards,
Carlo

4. Nikolay Dimitrov
Jul 08, 2014 @ 05:50:20

Solution 11 (Antal Koos, Python) – The file kty.py is missing, so code generation won’t work. Still the C solution is OK.

• kantal
Jul 08, 2014 @ 09:35:10

Hi!
The kty.py is in my solution#5, I didn’t want it to duplicate.

• Nikolay Dimitrov
Jul 08, 2014 @ 11:22:01

Ahh, sorry for the misunderstanding. I just missed the part that these 2 solutions had the same author :). Thanks for pointing this out.

5. Nikolay Dimitrov
Jul 08, 2014 @ 05:55:42

Solution 8 (Ohmohm, Python) – In file ptc01.py, lines 55-58, there’s a wrong validation of input value, which will cause the entire program to not work for any useful value of the resistance (the resistance is checked against range [-55, 150], while it should have been something like [490, 2211]).

• ultimateohm
Jul 08, 2014 @ 18:42:37

Ohh, sorry. I’ve just discovered that my code is reverse of function that should be.

6. Nikolay Dimitrov
Jul 08, 2014 @ 06:03:08

OK, so here are the solutions which I liked the most:

3. Marius Staciu, JavaScript
4. Peter Lynch, Perl
6. Marc, C
15. Mark Butowski, C

I’m leaving to the dear reader to find out why. And the usual disclaimer – this is my personal subjective opinion, feel free to disagree with me (in a respectful way).

Kind regards.