Zig: completed Leap and Difference of Squares

This commit is contained in:
Andrew Scott 2024-08-01 10:04:02 -04:00
parent c8ef7df34c
commit ddebf2f85b
Signed by: a
GPG key ID: 7CD5A5977E4931C1
12 changed files with 341 additions and 0 deletions

View file

@ -0,0 +1,19 @@
{
"authors": [
"massivelivefun"
],
"files": {
"solution": [
"difference_of_squares.zig"
],
"test": [
"test_difference_of_squares.zig"
],
"example": [
".meta/example.zig"
]
},
"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"
}

View file

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

View file

@ -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 Guide][zig-guide] 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-guide]: https://zig.guide/
[ziglings]: https://codeberg.org/ziglings/exercises

View file

@ -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

View file

@ -0,0 +1,19 @@
pub fn squareOfSum(number: usize) usize {
var sum: usize = 0;
for (0..number + 1) |i| {
sum += i;
}
return sum * 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);
}

View file

@ -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);
}

View file

@ -0,0 +1,19 @@
{
"authors": [
"massivelivefun"
],
"files": {
"solution": [
"leap.zig"
],
"test": [
"test_leap.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Given a year, report if it is a leap year.",
"source": "CodeRanch Cattle Drive, Assignment 3",
"source_url": "https://coderanch.com/t/718816/Leap"
}

View file

@ -0,0 +1 @@
{"track":"zig","exercise":"leap","id":"c311a440c14b4ef084adf8482af46d12","url":"https://exercism.org/tracks/zig/exercises/leap","handle":"Chomp1295","is_requester":true,"auto_approve":false}

53
zig/leap/HELP.md Normal file
View file

@ -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 leap.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

37
zig/leap/README.md Normal file
View file

@ -0,0 +1,37 @@
# Leap
Welcome to Leap on Exercism's Zig Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Given a year, report if it is a leap year.
The tricky thing here is that a leap year in the Gregorian calendar occurs:
```text
on every year that is evenly divisible by 4
except every year that is evenly divisible by 100
unless the year is also evenly divisible by 400
```
For example, 1997 is not a leap year, but 1996 is.
1900 is not a leap year, but 2000 is.
## Notes
Though our exercise adopts some very simple rules, there is more to learn!
For a delightful, four minute explanation of the whole leap year phenomenon, go watch [this youtube video][video].
[video]: https://www.youtube.com/watch?v=xX96xng7sAE
## Source
### Created by
- @massivelivefun
### Based on
CodeRanch Cattle Drive, Assignment 3 - https://coderanch.com/t/718816/Leap

12
zig/leap/leap.zig Normal file
View file

@ -0,0 +1,12 @@
pub fn isLeapYear(year: u32) bool {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
}
return false;
}
return true;
}
return false;
}

40
zig/leap/test_leap.zig Normal file
View file

@ -0,0 +1,40 @@
const std = @import("std");
const testing = std.testing;
const leap = @import("leap.zig");
test "year not divisible by 4 in common year" {
try testing.expect(!leap.isLeapYear(2015));
}
test "year divisible by 2, not divisible by 4 in common year" {
try testing.expect(!leap.isLeapYear(1970));
}
test "year divisible by 4, not divisible by 100 in leap year" {
try testing.expect(leap.isLeapYear(1996));
}
test "year divisible by 4 and 5 is still a leap year" {
try testing.expect(leap.isLeapYear(1960));
}
test "year divisible by 100, not divisible by 400 in common year" {
try testing.expect(!leap.isLeapYear(2100));
}
test "year divisible by 100 but not by 3 is still not a leap year" {
try testing.expect(!leap.isLeapYear(1900));
}
test "year divisible by 400 is leap year" {
try testing.expect(leap.isLeapYear(2000));
}
test "year divisible by 400 but not by 125 is still a leap year" {
try testing.expect(leap.isLeapYear(2400));
}
test "year divisible by 200, not divisible by 400 in common year" {
try testing.expect(!leap.isLeapYear(1800));
}