Skip to content

Commit

Permalink
Implement collatz-conjecture in zig
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgoettschkes committed Oct 5, 2023
1 parent d0a2589 commit c6d9833
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
53 changes: 53 additions & 0 deletions zig/collatz-conjecture/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 collatz_conjecture.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
48 changes: 48 additions & 0 deletions zig/collatz-conjecture/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Collatz Conjecture

Welcome to Collatz Conjecture on Exercism's Zig Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

The Collatz Conjecture or 3x+1 problem can be summarized as follows:

Take any positive integer n.
If n is even, divide n by 2 to get n / 2.
If n is odd, multiply n by 3 and add 1 to get 3n + 1.
Repeat the process indefinitely.
The conjecture states that no matter which number you start with, you will always reach 1 eventually.

Given a number n, return the number of steps required to reach 1.

## Examples

Starting with n = 12, the steps would be as follows:

0. 12
1. 6
2. 3
3. 10
4. 5
5. 16
6. 8
7. 4
8. 2
9. 1

Resulting in 9 steps.
So for input n = 12, the return value would be 9.

## Source

### Created by

- @massivelivefun

### Contributed to by

- @ee7

### Based on

An unsolved problem in mathematics named after mathematician Lothar Collatz - https://en.wikipedia.org/wiki/3x_%2B_1_problem
22 changes: 22 additions & 0 deletions zig/collatz-conjecture/collatz_conjecture.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub const ComputationError = error {
IllegalArgument
};

pub fn steps(number: usize) anyerror!usize {
if (number < 1) {
return ComputationError.IllegalArgument;
}

var count: usize = 0;
var curNum: usize = number;
while(curNum != 1) {
count += 1;
if (curNum % 2 == 0) {
curNum = curNum / 2;
} else {
curNum = curNum * 3 + 1;
}
}

return count;
}
35 changes: 35 additions & 0 deletions zig/collatz-conjecture/test_collatz_conjecture.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const std = @import("std");
const testing = std.testing;

const collatz_conjecture = @import("collatz_conjecture.zig");
const ComputationError = collatz_conjecture.ComputationError;

test "zero steps for one" {
const expected: usize = 0;
const actual = try collatz_conjecture.steps(1);
try testing.expectEqual(expected, actual);
}

test "divide if even" {
const expected: usize = 4;
const actual = try collatz_conjecture.steps(16);
try testing.expectEqual(expected, actual);
}

test "even and odd steps" {
const expected: usize = 9;
const actual = try collatz_conjecture.steps(12);
try testing.expectEqual(expected, actual);
}

test "large number of even and odd steps" {
const expected: usize = 152;
const actual = try collatz_conjecture.steps(1_000_000);
try testing.expectEqual(expected, actual);
}

test "zero is an error" {
const expected = ComputationError.IllegalArgument;
const actual = collatz_conjecture.steps(0);
try testing.expectError(expected, actual);
}

0 comments on commit c6d9833

Please sign in to comment.