Get Time
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
pi code | Reply
The code:
	if (precision==1) return "3.1";
	if (precision==2) return "3.14";
	if (precision==3) return "3.142";

should be replaced with:
string pi = "3.1415926535897932384626433832795028841971";
return pi.substring(0, precision + 2);
Re: pi code (response to post by | Reply
In that problem you should also round the last digit, so your solution will not work.
Re: pi code (response to post by OlexiyO) | Reply
Ok, it can be easily changed to solve that, but a 25 if statement solution shouldn't be suggested in any educational content article.
Re: pi code (response to post by | Reply
Works for me!
Re: pi code (response to post by HardCoder) | Reply
:) yeah but you have a different purpose recently, most people want to increase their rating, not generate forum threads about their solutions :P. These type of solutions are useful in SRMs when the algorithm for getting a result takes more than 2 seconds, the rest of the time they just decrease your score.
Re: pi code (response to post by | Reply
It also required you to have exactly those many digits.
My code was something in between: here :-)
Re: pi code (response to post by aboyner) | Reply
So my code is between yours and HardCoders, since for some reason it has the HardCodered value for 12 :)

[Edit]I don't know how could I miss that so have you.
Re: pi code (response to post by slex) | Reply
Since aboyner's also has the answer for 12 hardcoded, I think yours is between his and his.
Re: pi code (response to post by Kawigi) | Reply
I wish I were there when easy ones were really easy :(
import java.math.BigDecimal;
public class PiCalculator {
	public String calculate(int precision) {
		BigDecimal easy = new BigDecimal("3.141592653589793238462643383279");
		easy = easy.divide(BigDecimal.ONE, precision, BigDecimal.ROUND_HALF_UP);
		return easy.toString();
Re: pi code (response to post by darko_aleksic) | Reply
My favorite (too easy) one was also mentioned by the feature - FormatAmt could be solved in Java by "return java.text.NumberFormat.getCurrencyInstance().format(dollars + cents/100.0);" Of course, you had to know that's what it did and you had to assume that TopCoder runs your code under a US locale. Too bad I wasn't in that SRM, because I learned that over and over again when I was a TA :-)
Re: pi code (response to post by Kawigi) | Reply
Tad shorter in VB:
return format(dollars+(cents/100),"$###,###,###,##0.00")