Browse Source

Merge branch '14-difference-of-squares'

Frédéric G. MARAND 7 months ago
parent
commit
8da155c629

+ 27 - 0
difference-of-squares/.exercism/config.json

@@ -0,0 +1,27 @@
+{
+  "authors": [],
+  "contributors": [
+    "arueckauer",
+    "dkinzer",
+    "kunicmarko20",
+    "kytrinyx",
+    "lafent",
+    "petemcfarlane",
+    "Scientifica96",
+    "mk-mxp"
+  ],
+  "files": {
+    "solution": [
+      "DifferenceOfSquares.php"
+    ],
+    "test": [
+      "DifferenceOfSquaresTest.php"
+    ],
+    "example": [
+      ".meta/example.php"
+    ]
+  },
+  "blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.",
+  "source": "Problem 6 at Project Euler",
+  "source_url": "https://projecteuler.net/problem=6"
+}

+ 1 - 0
difference-of-squares/.exercism/metadata.json

@@ -0,0 +1 @@
+{"track":"php","exercise":"difference-of-squares","id":"0d9ecc074b1c4bce99fd692c81e91aed","url":"https://exercism.org/tracks/php/exercises/difference-of-squares","handle":"Fairgame","is_requester":true,"auto_approve":false}

+ 42 - 0
difference-of-squares/DifferenceOfSquares.php

@@ -0,0 +1,42 @@
+<?php
+
+/*
+ * By adding type hints and enabling strict type checking, code can become
+ * easier to read, self-documenting and reduce the number of potential bugs.
+ * By default, type declarations are non-strict, which means they will attempt
+ * to change the original type to match the type specified by the
+ * type-declaration.
+ *
+ * In other words, if you pass a string to a function requiring a float,
+ * it will attempt to convert the string value to a float.
+ *
+ * To enable strict mode, a single declare directive must be placed at the top
+ * of the file.
+ * This means that the strictness of typing is configured on a per-file basis.
+ * This directive not only affects the type declarations of parameters, but also
+ * a function's return type.
+ *
+ * For more info review the Concept on strict type checking in the PHP track
+ * <link>.
+ *
+ * To disable strict typing, comment out the directive below.
+ */
+
+declare(strict_types=1);
+
+function squareOfSum(int $max): int {
+  $res = $max * ($max + 1) / 2;
+  return $res * $res;
+}
+
+function sumOfSquares(int $max): int {
+  $sum = 0;
+  for ($i = 1; $i <= $max; $i++) {
+    $sum += $i * $i;
+  }
+  return $sum;
+}
+
+function difference(int $max): int {
+  return squareOfSum($max) - sumOfSquares($max);
+}

+ 92 - 0
difference-of-squares/DifferenceOfSquaresTest.php

@@ -0,0 +1,92 @@
+<?php
+
+declare(strict_types=1);
+
+class DifferenceOfSquaresTest extends PHPUnit\Framework\TestCase
+{
+    public static function setUpBeforeClass(): void
+    {
+        require_once 'DifferenceOfSquares.php';
+    }
+
+    /**
+     * uuid e46c542b-31fc-4506-bcae-6b62b3268537
+     * @testdox Square of sum 1
+     */
+    public function testSquareOfSumTo1(): void
+    {
+        $this->assertEquals(1, squareOfSum(1));
+    }
+
+    /**
+     * uuid 9b3f96cb-638d-41ee-99b7-b4f9c0622948
+     * @testdox Square of sum 5
+     */
+    public function testSquareOfSumTo5(): void
+    {
+        $this->assertEquals(225, squareOfSum(5));
+    }
+
+    /**
+     * uuid 54ba043f-3c35-4d43-86ff-3a41625d5e86
+     * @testdox Square of sum 100
+     */
+    public function testSquareOfSumTo100(): void
+    {
+        $this->assertEquals(25502500, squareOfSum(100));
+    }
+
+    /**
+     * uuid 01d84507-b03e-4238-9395-dd61d03074b5
+     * @testdox Sum of squares 1
+     */
+    public function testSumOfSquaresTo1(): void
+    {
+        $this->assertEquals(1, sumOfSquares(1));
+    }
+
+    /**
+     * uuid c93900cd-8cc2-4ca4-917b-dd3027023499
+     * @testdox Sum of squares 5
+     */
+    public function testSumOfSquaresTo5(): void
+    {
+        $this->assertEquals(55, sumOfSquares(5));
+    }
+
+    /**
+     * uuid 94807386-73e4-4d9e-8dec-69eb135b19e4
+     * @testdox Sum of squares 100
+     */
+    public function testSumOfSquaresTo100(): void
+    {
+        $this->assertEquals(338350, sumOfSquares(100));
+    }
+
+    /**
+     * uuid 44f72ae6-31a7-437f-858d-2c0837adabb6
+     * @testdox Difference of squares 1
+     */
+    public function testDifferenceOfSumTo1(): void
+    {
+        $this->assertEquals(0, difference(1));
+    }
+
+    /**
+     * uuid 005cb2bf-a0c8-46f3-ae25-924029f8b00b
+     * @testdox Difference of squares 5
+     */
+    public function testDifferenceOfSumTo5(): void
+    {
+        $this->assertEquals(170, difference(5));
+    }
+
+    /**
+     * uuid b1bf19de-9a16-41c0-a62b-1f02ecc0b036
+     * @testdox Difference of squares 100
+     */
+    public function testDifferenceOfSumTo100(): void
+    {
+        $this->assertEquals(25164150, difference(100));
+    }
+}

+ 52 - 0
difference-of-squares/HELP.md

@@ -0,0 +1,52 @@
+# Help
+
+## Running the tests
+
+## Running the tests
+
+1. Go to the root of your PHP exercise directory, which is `<EXERCISM_WORKSPACE>/php`.
+   To find the Exercism workspace run
+
+       ➜ exercism debug | grep Workspace
+
+1. Get [PHPUnit] if you don't have it already.
+
+       ➜ wget -O phpunit https://phar.phpunit.de/phpunit-9.phar
+       ➜ chmod +x phpunit
+       ➜ ./phpunit --version
+
+2. Execute the tests:
+
+       ➜ ./phpunit file_to_test.php
+
+   For example, to run the tests for the Hello World exercise, you would run:
+
+       ➜ ./phpunit HelloWorldTest.php
+
+[PHPUnit]: https://phpunit.de
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit DifferenceOfSquares.php` command.
+This command will upload your solution to the Exercism website and print the solution page's URL.
+
+It's possible to submit an incomplete solution which allows you to:
+
+- See how others have completed the exercise
+- Request help from a mentor
+
+## Need to get help?
+
+If you'd like help solving the exercise, check the following pages:
+
+- The [PHP track's documentation](https://exercism.org/docs/tracks/php)
+- The [PHP track's programming category on the forum](https://forum.exercism.org/c/programming/php)
+- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
+
+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
+
+To get help if you're having trouble, you can use one of the following resources:
+
+ - [/r/php](https://www.reddit.com/r/php) is the PHP subreddit.
+ - [StackOverflow](https://stackoverflow.com/questions/tagged/php) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

+ 36 - 0
difference-of-squares/README.md

@@ -0,0 +1,36 @@
+# Difference of Squares
+
+Welcome to Difference of Squares on Exercism's PHP Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Instructions
+
+Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.
+
+The square of the sum of the first ten natural numbers is
+(1 + 2 + ... + 10)² = 55² = 3025.
+
+The sum of the squares of the first ten natural numbers is
+1² + 2² + ... + 10² = 385.
+
+Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640.
+
+You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged.
+Finding the best algorithm for the problem is a key skill in software engineering.
+
+## Source
+
+### Contributed to by
+
+- @arueckauer
+- @dkinzer
+- @kunicmarko20
+- @kytrinyx
+- @lafent
+- @petemcfarlane
+- @Scientifica96
+- @mk-mxp
+
+### Based on
+
+Problem 6 at Project Euler - https://projecteuler.net/problem=6