Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6882005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:12:01+00:00 2026-05-27T05:12:01+00:00

This is fairly ‘math-y’ but I’m posting here because it’s a Project Euler problem,

  • 0

This is fairly ‘math-y’ but I’m posting here because it’s a Project Euler problem, & I have working code that presumably has bugs in it.

The question Determing longest repeating cycle in a decimal expansion solves the problem using logarithms, but I’m interested in solving with simple brute force. More accurately, I’m interested in understanding why my algorithm and code is not returning the correct solution.

The algorithm is simple:

  • replicate a ‘long division’,
  • at each step record the divisor and the remainder
  • when a divisor / remainder tuple is repeated, infer that the decimal representation will repeat.

Here are private fields, as requested

private int numerator; 
private int recurrence; 
private int result; 
private int resultRecurrence; 
private List<dynamic> digits;

and here is the code:

private void Go()
{
    foreach (var i in primes)
    {
        digits = new List<dynamic>();
        numerator = 1;
        recurrence = 0;

        while (numerator != 0)
        {
            numerator *= 10;

            // quotient
            var q = numerator / i;

            // remainder
            var r = numerator % i;
            digits.Add(new { Divisor = q, Remainder = r });

            // if we've found a repetition then break out
            var m = digits.Where(p => p.Divisor == q && p.Remainder == r).ToList();
            if (m.Count > 1)
            {
                recurrence = digits.LastIndexOf(m[0]) - digits.IndexOf(m[0]);
                break;
            }
            numerator = r;
        }

        if (recurrence > resultRecurrence)
        {
            resultRecurrence = recurrence;
            result = i;
        }
}}

When testing integers < 10 and < 20 I get the correct result; and I correctly identify the value of i as well. However the decimal represetation that I get is incorrect – I calculate i-1 whereas the correct result is far less (something like i-250).

So presumably I either have a programming bug – which I can’t find – or a logic bug.

I’m confused because it feels like a multiplicative group over p to me, in which there would be p-1 elements. I’m sure I’m missing something, can anyone provide suggestions?

edit

I’m not going to include my prime number code – it’s not relevant, as I explain above I correctly identify the value of i (from memory it is 983) but I’m having problems getting the correct value for resultRecurrence.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:12:02+00:00Added an answer on May 27, 2026 at 5:12 am

    I’m confused because it feels like a multiplicative group over p to me, in which there would be p-1 elements. I’m sure I’m missing something, can anyone provide suggestions?

    Close.

    For all primes except 2 and 5 (which divide 10), the sequence of remainders is formed by starting with 1 and transforming by

    remainder = (10 * remainder) % prime
    

    thus the k-th remainder is 10k (mod prime) and the set of remainders forms a subgroup of the group of nonzero remainders modulo prime[1]. The length of the recurring cycle is the order of that subgroup, which is also known as the order of 10 modulo prime.

    The order of the group of nonzero remainders modulo prime is prime-1, and there’s a theorem by Fermat:

    Let G be a finite group of order g and H be a subgroup of G. Then the order h of H divides g.

    So the length of the cycle is always a divisor of prime-1, and sometimes it’s prime-1, e.g. for 7 or 19.

    [1] For composite numbers n coprime to 10, that would be the group of remainders modulo n that are coprime to n.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked into this fairly extensively, but have not found quite what I
I trying to figure out how to solve this (fairly) simple problem but I
I know this is fairly subjective, but I'm diving into testing and learning about
I guess this is fairly simple for you but i cant wrap my head
I'm sure this is fairly simple, however I have a major mental block on
This is a fairly trivial matter, but I'm curious to hear people's opinions on
This is a fairly common problem, it probably has a name, I just don't
This looks fairly simple, but 30 minutes of googling took me nowhere. :-( How
This is fairly trivial, but it bothers me to no end that I haven't
I have this fairly simple function to compute the mean of elements of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.