Skip to content

Commit

Permalink
Implement difference-of-squares in zig
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgoettschkes committed Oct 4, 2023
1 parent d2d8f6c commit 1af0e71
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
53 changes: 53 additions & 0 deletions zig/difference-of-squares/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Help

## Running the tests

Write your code in `<exercise_name>.zig`.

To run the tests for an exercise, run:

```bash
zig test test_exercise_name.zig
```

in the exercise's root directory (replacing `exercise_name` with the name of the exercise).

## Submitting your solution

You can submit your solution using the `exercism submit difference_of_squares.zig` 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 [Zig track's documentation](https://exercism.org/docs/tracks/zig)
- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/zig)
- [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.

- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
- [Zig Learn][zig-learn] is an excellent primer that explains the language features that Zig has to offer.
- [Ziglings][ziglings] is highly recommended.
Learn Zig by fixing tiny broken programs.
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
- [/r/Zig][reddit] is the main Zig subreddit.
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.

[discord]: https://discordapp.com
[discord-zig]: https://discord.com/invite/gxsFFjE
[documentation]: https://ziglang.org/documentation/master
[irc]: https://webchat.freenode.net/?channels=%23zig
[reddit]: https://www.reddit.com/r/Zig
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
[zig-learn]: https://ziglearn.org/
[ziglings]: https://github.com/ratfactor/ziglings
29 changes: 29 additions & 0 deletions zig/difference-of-squares/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Difference of Squares

Welcome to Difference of Squares on Exercism's Zig 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

### Created by

- @massivelivefun

### Based on

Problem 6 at Project Euler - https://projecteuler.net/problem=6
23 changes: 23 additions & 0 deletions zig/difference-of-squares/difference_of_squares.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub fn squareOfSum(number: usize) usize {
var sum: usize = 0;
for (1..number + 1) |i| {
sum += i;
}

sum = sum * sum;

return sum;
}

pub fn sumOfSquares(number: usize) usize {
var sum: usize = 0;
for (1..number + 1) |i| {
sum += i * i;
}

return sum;
}

pub fn differenceOfSquares(number: usize) usize {
return squareOfSum(number) - sumOfSquares(number);
}
58 changes: 58 additions & 0 deletions zig/difference-of-squares/test_difference_of_squares.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const std = @import("std");
const testing = std.testing;

const difference_of_squares = @import("difference_of_squares.zig");

test "square of sum up to 1" {
const expected: usize = 1;
const actual = difference_of_squares.squareOfSum(1);
try testing.expectEqual(expected, actual);
}

test "square of sum up to 5" {
const expected: usize = 225;
const actual = difference_of_squares.squareOfSum(5);
try testing.expectEqual(expected, actual);
}

test "square of sum up to 100" {
const expected: usize = 25_502_500;
const actual = difference_of_squares.squareOfSum(100);
try testing.expectEqual(expected, actual);
}

test "sum of squares up to 1" {
const expected: usize = 1;
const actual = difference_of_squares.sumOfSquares(1);
try testing.expectEqual(expected, actual);
}

test "sum of squares up to 5" {
const expected: usize = 55;
const actual = difference_of_squares.sumOfSquares(5);
try testing.expectEqual(expected, actual);
}

test "sum of squares up to 100" {
const expected: usize = 338_350;
const actual = difference_of_squares.sumOfSquares(100);
try testing.expectEqual(expected, actual);
}

test "difference of squares up to 1" {
const expected: usize = 0;
const actual = difference_of_squares.differenceOfSquares(1);
try testing.expectEqual(expected, actual);
}

test "difference of squares up to 5" {
const expected: usize = 170;
const actual = difference_of_squares.differenceOfSquares(5);
try testing.expectEqual(expected, actual);
}

test "difference of squares up to 100" {
const expected: usize = 25_164_150;
const actual = difference_of_squares.differenceOfSquares(100);
try testing.expectEqual(expected, actual);
}

0 comments on commit 1af0e71

Please sign in to comment.