From c60dd2574310ad7917390aedc740eb22cbc8310f Mon Sep 17 00:00:00 2001 From: RohanK6 Date: Tue, 6 Oct 2020 22:22:33 -0400 Subject: [PATCH 1/3] Implemented Heron's Formula --- Maths/HeronsFormula.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Maths/HeronsFormula.java diff --git a/Maths/HeronsFormula.java b/Maths/HeronsFormula.java new file mode 100644 index 000000000000..de84aedf0a63 --- /dev/null +++ b/Maths/HeronsFormula.java @@ -0,0 +1,34 @@ +package Maths; + +/** + * Heron's formula gives the area of a triangle when the length of all three sides are known. + */ + +public class HeronsFormula { + /** + * + * @param a a side of the triangle + * @param b a side of the triangle + * @param c a side of the triangle + * @return the calculated area of the triangle using Heron's formula + */ + public static double calculateArea(double a, double b, double c) { + /* Checks if the three sides can really represent a triangle */ + if ((a + b < c) || (b + c < a) || (a + c < b)) { + return 0.0; + } + + double halfPerimeter = (a + b + c) / 2.0; + double area = Math.sqrt(halfPerimeter * (halfPerimeter - a) * (halfPerimeter - b) * (halfPerimeter - c)); + + return area; + } + /** + * Driver code + */ + public static void main(String[] args) { + assert calculateArea(5, 5, 5) == 10.825317547305483; + assert calculateArea(4, 4, 5) == 7.806247497997997; + assert calculateArea(10, 2, 3) == 0.0; + } +} From 45738d8af50665d10b456e64c1335c6b7e8d7382 Mon Sep 17 00:00:00 2001 From: RohanK6 Date: Tue, 6 Oct 2020 22:29:49 -0400 Subject: [PATCH 2/3] Added WikiWand link for Heron's Formula --- Maths/HeronsFormula.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Maths/HeronsFormula.java b/Maths/HeronsFormula.java index de84aedf0a63..aafa1769ea2f 100644 --- a/Maths/HeronsFormula.java +++ b/Maths/HeronsFormula.java @@ -2,6 +2,7 @@ /** * Heron's formula gives the area of a triangle when the length of all three sides are known. + * https://www.wikiwand.com/en/Heron%27s_formula */ public class HeronsFormula { From 6757a9a33e73df33b0621126376cc882ca398387 Mon Sep 17 00:00:00 2001 From: RohanK6 Date: Wed, 7 Oct 2020 02:07:54 -0400 Subject: [PATCH 3/3] Implements Collatz Sequence --- Maths/CollatzSequence.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Maths/CollatzSequence.java diff --git a/Maths/CollatzSequence.java b/Maths/CollatzSequence.java new file mode 100644 index 000000000000..f03d898d857e --- /dev/null +++ b/Maths/CollatzSequence.java @@ -0,0 +1,38 @@ +package Maths; + +/** + * The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: + * start with any positive integer n. + * Then each term is obtained from the previous term as follows: + * if the previous term is even, the next term is one half of the previous term. + * If the previous term is odd, the next term is 3 times the previous term plus 1. + * The conjecture is that no matter what value of n, the sequence will always reach 1. + * https://www.wikiwand.com/en/Collatz_conjecture + */ +public class CollatzSequence { + /** + * Runs the sequence starting on the value passed in, stopping when the sequence reaches 1. + * @param start initial value the sequence is run on. + */ + public static void collatz(int start) { + while (start != 1) { + System.out.print(start + " "); + if (start % 2 == 0) { + start = start / 2; + } + else { + start = 3 * start + 1; + } + } + System.out.print(start); + System.out.println(); + } + + /** + * Driver code + */ + public static void main(String[] args) { + collatz(3); // 3, 10, 5, 16, 8, 4, 2, 1 + collatz(6); // 6, 3, 10, 5, 16, 8, 4, 2, 1 + } +}