From 355d4c1fee55b176f8309b1558b69f858329b8f5 Mon Sep 17 00:00:00 2001 From: Deepak Date: Mon, 2 Oct 2017 13:00:39 +0530 Subject: [PATCH 1/3] added precision_root_algo --- Misc/root_precision | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Misc/root_precision diff --git a/Misc/root_precision b/Misc/root_precision new file mode 100644 index 000000000000..1b5e95478209 --- /dev/null +++ b/Misc/root_precision @@ -0,0 +1,28 @@ +import java.io.*; +import java.util.*; +import java.text.*; +import java.math.*; +import java.util.regex.*; + +public class Solution { + + public static void main(String[] args) { + Scanner scn = new Scanner(System.in); + + int N = scn.nextInt(); + int P = scn.nextInt(); + + System.out.println(squareRoot(N, P)); + } + + public static double squareRoot(int N, int P) { + double sqrt = 0;; + + // Write your code here + double root = Math.pow(N, 0.5); + int pre = (int) Math.pow(10, P); + root = root * pre; + sqrt = (int)root; + return (double)sqrt/pre; + } +} From b7a9d0de4435594e40fad18a6db1b406830430fa Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 3 Oct 2017 08:28:04 +0530 Subject: [PATCH 2/3] rename --- Misc/{root_precision => root_precision.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/{root_precision => root_precision.java} (100%) diff --git a/Misc/root_precision b/Misc/root_precision.java similarity index 100% rename from Misc/root_precision rename to Misc/root_precision.java From 32d03516fe647d47655e16676bc114a9428083cd Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 3 Oct 2017 13:12:50 +0530 Subject: [PATCH 3/3] update added comments and variable names are more simpler now --- Misc/root_precision.java | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Misc/root_precision.java b/Misc/root_precision.java index 1b5e95478209..0ae00de0fbf3 100644 --- a/Misc/root_precision.java +++ b/Misc/root_precision.java @@ -7,22 +7,27 @@ public class Solution { public static void main(String[] args) { + //take input Scanner scn = new Scanner(System.in); - int N = scn.nextInt(); - int P = scn.nextInt(); + int N = scn.nextInt(); //N is the input number + int P = scn.nextInt(); //P is precision value for eg - P is 3 in 2.564 and 5 in 3.80870. System.out.println(squareRoot(N, P)); } public static double squareRoot(int N, int P) { - double sqrt = 0;; - - // Write your code here - double root = Math.pow(N, 0.5); - int pre = (int) Math.pow(10, P); - root = root * pre; - sqrt = (int)root; - return (double)sqrt/pre; + double rv = 0; //rv means return value + + double root = Math.pow(N, 0.5); + + //calculate precision to power of 10 and then multiply it with root value. + int precision = (int) Math.pow(10, P); + root = root * precision; + /*typecast it into integer then divide by precision and again typecast into double + so as to have decimal points upto P precision */ + + rv = (int)root; + return (double)rv/precision; } }