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

  • SEARCH
  • Home
  • 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 8152747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:48:10+00:00 2026-06-06T15:48:10+00:00

How to detect repeating digits in an infinite sequence? I tried Floyd & Brent

  • 0

How to detect repeating digits in an infinite sequence? I tried Floyd & Brent detection algorithm but come to nothing…
I have a generator that yields numbers ranging from 0 to 9 (inclusive) and I have to recognize a period in it.

Example test case:

import itertools

# of course this is a fake one just to offer an example
def source():
    return itertools.cycle((1, 0, 1, 4, 8, 2, 1, 3, 3, 1))

>>> gen = source()
>>> period(gen)
(1, 0, 1, 4, 8, 2, 1, 3, 3, 1)
  • 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-06-06T15:48:12+00:00Added an answer on June 6, 2026 at 3:48 pm

    Empirical methods

    Here’s a fun take on the problem. The more general form of your question is this:

    Given a repeating sequence of unknown length, determine the period of
    the signal.

    The process to determine the repeating frequencies is known as the Fourier Transform. In your example case the signal is clean and discrete, but the following solution will work even with continuous noisy data! The FFT will try to duplicate the frequencies of the input signal by approximating them in the so-called "wave-space" or "Fourier-space". Basically a peak in this space corresponds to a repeating signal. The period of your signal is related to the longest wavelength that is peaked.

    import itertools
    
    # of course this is a fake one just to offer an example
    def source():
        return itertools.cycle((1, 0, 1, 4, 8, 2, 1, 3, 3, 2))
    
    import pylab as plt
    import numpy as np
    import scipy as sp
    
    # Generate some test data, i.e. our "observations" of the signal
    N = 300
    vals = source()
    X = np.array([vals.next() for _ in xrange(N)])
    
    # Compute the FFT
    W    = np.fft.fft(X)
    freq = np.fft.fftfreq(N,1)
     
    # Look for the longest signal that is "loud"
    threshold = 10**2
    idx = np.where(abs(W)>threshold)[0][-1]
    
    max_f = abs(freq[idx])
    print "Period estimate: ", 1/max_f
    

    This gives the correct answer for this case, 10 though if N didn’t divide the cycles cleanly, you would get a close estimate. We can visualize this via:

    plt.subplot(211)
    plt.scatter([max_f,], [np.abs(W[idx]),], s=100,color='r')
    plt.plot(freq[:N/2], abs(W[:N/2]))
    plt.xlabel(r"$f$")
    
    plt.subplot(212)
    plt.plot(1.0/freq[:N/2], abs(W[:N/2]))
    plt.scatter([1/max_f,], [np.abs(W[idx]),], s=100,color='r')
    plt.xlabel(r"$1/f$")
    plt.xlim(0,20)
    
    plt.show()
    

    enter image description here

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

Sidebar

Related Questions

Possible Duplicate: Detect multitouch with Ontouch listener Android I have a question about detecting
I have a series of data and need to detect peak values in the
I'm trying to do something after scrolling stopped.So, I tried using OnScrollListener#onScrollStateChanged(SCROLL_STATE_IDLE) to detect
I have the following regex to detect text inside <?php (including the tag) '/<\?php(.*)\?>/isU'
I'd like to apply a filter at the Controller level, but only have it's
I detect if a user has chrome frame by placing this in teh body
To detect rotation of the mouse wheel in .NET/WinForms, I can override OnMouseWheel .
I want to detect cases where an Ajax call is in progress and then
I am trying to detect groups which contain the difference between first age and
I'm trying to detect and remove those fields that after a sync are still

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.