Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crazy Magic, #26 #21

Closed
brenschi opened this issue May 22, 2019 · 2 comments
Closed

Crazy Magic, #26 #21

brenschi opened this issue May 22, 2019 · 2 comments

Comments

@brenschi
Copy link

Hi, I'm trying to understand what kind of crazy magic you're using for the twenty-sixth problem, specifically this little black box of black magic:

def reciprocal_cycle_len(n):
    seen = {}
    x = 1

    for i in itertools.count():
        if x in seen:
            return i - seen[x]
        else:
            seen[x] = i
            x = x * 10 % n

It's the simplest thing I've ever seen that I just can't figure out. I guess I'm just not good with numerology. Is there a spatial relationship that would make sense?

@nayuki
Copy link
Owner

nayuki commented May 26, 2019

It is a finite state machine that simulates dividing 1.000000... by n. In each iteration, x is the current remainder in the division, which is an integer between 1 and n-1 (inclusive).

This thread is about your understanding of math and logic, not an issue with my program. Please direct any further questions you have to a public volunteer forum such as https://stackoverflow.com/questions/ask or https://math.stackexchange.com/questions/ask .

@nayuki nayuki closed this as completed May 26, 2019
@brenschi
Copy link
Author

It is a finite state machine that simulates dividing 1.000000... by n. In each iteration, x is the current remainder in the division, which is an integer between 1 and n-1 (inclusive).

Thanks, bro.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants