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 6031937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:15:33+00:00 2026-05-23T05:15:33+00:00

Say I have a function f: f(0) = 0 f(i) = (i – 1)

  • 0

Say I have a function f:

f(0) = 0
f(i) = (i - 1) % 4

f(0..12):

0 0 1 2 3 0 1 2 3 0 1 2 3

I want to find the cycle start and the cycle length, which are 1 and 4, respectively.
The tortoise and hare algorithm works with iterated functions, but I don’t have an iterated function.
Are there other algorithms that work with non-iterated functions or can the tortoise and hare algorithm be modified for this?

Edit:

Using Jason S’s answer, I managed to come up with this, which seems to be working:

public static Tuple<int, int> ModifiedTortoiseHare(Func<int, int> f, int x0 = 0, int checks = 4)
{
    for (; ; x0++)
    {
        int lam = 0, tortoise, hare;

        do
        {
            lam++;
            tortoise = f(x0 + lam);
            hare = f(x0 + 2 * lam);
        } while (tortoise != hare);

        int mu = -1;

        do
        {
            mu++;
            tortoise = f(x0 + mu);
            hare = f(x0 + mu + lam);
        } while (tortoise != hare);

        if (mu != 0) continue;

        bool correct = true;
        int lamCheckMax = lam * checks;

        for (int x = 0; x < lamCheckMax; x++)
        {
            if (f(x0 + x + mu) != f(x0 + x + mu + lam))
            {
                correct = false;
                if (mu != 0) x0 += mu - 1;
                break;
            }
        }

        if (correct) return Tuple.Create(x0 + mu, lam);
    }
}
  • 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-23T05:15:34+00:00Added an answer on May 23, 2026 at 5:15 am

    If the function is a “black box”, and you have the ability to find f(x) for any individual x (whether valid for real numbers or only integers), but you don’t know anything else, there is no general way to find a cycle start and length. For example, consider the function

    f(k) = (k - 1) % 4 + g(k)
    g(k) = max(0, k-1000000)
    

    then f(k) looks like it repeats every 4 integers, but then when you get to k = 1000000, then the pattern stops.


    If the function has a finite range, and you can test for all integers, the tortoise/hare algorithm (= Floyd’s cycle-finding algorithm) can be used to help.

    Instead of iterating function evaluation, calculate f(k0 + k) and f(k0 + 2*k) until they match, at which point the suspected period is k, and you just need to repeat through all values to verify that the cycle continues.

    Your question appears to be an equivalent problem as “How should I find repeated word sequences?” which has a number of answers.

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

Sidebar

Related Questions

Say I have a function foo that I want to call n times. In
I'm sometimes confused to using which one of them, say I have a function
Say I have a C function which takes a variable number of arguments: How
Say I have a function: void someFunc(int *x,int count); which is out of my
So say I have a function. I want it to return a value when
Let's say I have this function, which is part of some gui toolkit: typedef
Say I have some function fn1() in Erlang which returns {ok, Result} if the
Lets say I have a function which takes a function pointer as a parameter,
let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment
Say I have a function func(i) that creates an object for an integer i,

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.