Skip to content
the poor coder

Codewars 1n- Cycle

Poor Man
Published
Reading time
2 min read
On this page

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:

Text

function cycle(n) {
  let remainders = [];
  let remainder = 1 % n;
  while (!remainders.includes(remainder)) {
    remainders.push(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!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC