Codewars 1n- Cycle

Codewars 1n - Cycle

If you love solving coding challenges and puzzles, then Codewars is one of the best platforms out there. It provides a wide range of coding challenges ranging from easy to hard levels. One of the most popular challenges on Codewars is the "1n - Cycle" challenge.

The Challenge

The challenge requires us to write a function that takes a positive integer as input and returns the length of its cycle when it's divided by 1/n. The cycle is defined as the sequence of remainders generated by the repeated division process.

My Experience

When I first came across this challenge, I was intrigued by it. It seemed simple at first, but as I dove deeper into it, I realized that it wasn't as easy as it seemed. I spent hours trying to come up with a solution, but I couldn't crack it.

After a lot of research and reading through various solutions, I finally came up with a solution that worked. Here's how I did it:

function cycle(n) {
  let remainders = [];
  let remainder = 1 % n;
  while (!remainders.includes(remainder)) {
    remainder = (remainder * 10) % n;
  return remainders.length - remainders.indexOf(remainder);

First, I initialized an empty array to store the remainders and set the initial remainder to 1. Then, I used a while loop to keep generating remainders until we encounter a remainder that we've already seen before. Each time we generate a remainder, we push it into the remainders array and update the remainder variable by multiplying it by 10 and taking modulus n. This process continues until we find a remainder that we've already seen before.

Finally, the function returns the length of the cycle, which is the difference between the length of the remainders array and the index of the first occurrence of the repeated remainder.

Alternative Solutions

There are various ways to solve this challenge, and here are a few alternative solutions:

  • One approach is to use Floyd's cycle-finding algorithm, also known as the "tortoise and hare" algorithm. This algorithm uses two pointers, one that moves one step at a time and another that moves two steps at a time. The algorithm stops when the two pointers meet, indicating the presence of a cycle.
  • Another approach is to use Brent's cycle detection algorithm, which is an optimized version of Floyd's algorithm. This algorithm uses a combination of slow and fast movements of pointers to detect cycles in a sequence.
  • Lastly, we can also solve this challenge by keeping track of the remainders in a hash table until we find a repeating remainder. This approach is similar to using an array but has a better time complexity for large inputs.

Coding challenges like "1n - Cycle" on Codewars are a great way to improve our coding skills and problem-solving abilities. I highly recommend giving them a try!

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]