Simple analysis algorithm

I am trying to get an algorithm up to complexity of at least O (n ^ (3/2)). Here is the algorithm:

function( string s, int n )
{
    bool result = false;
    int position = 0;
    for( int i = 0; i < n/2; i++ )
    {
        position = 0;
        for( int j = i+1; j < n; j++ )
        {
            if( s[j] != s[position] ) break;        
            if( j == n ) result = true;
            if( position == i ) position = 0;
            else position++;        
        }
        if(result) break;       
    }
    return result;
}

The first for-loop will iterate n / 2 times, which is O (n) complexity. I need the inside of the for-loop to have no more than O (sqrt (n)), thereby giving the whole algorithm complexity O (n ^ (3/2)). I am struggling to figure out if I need a nested loop for the right complexity.

.
n - . , s. s "0" "1". , "001001", "001" . . . , O (1) () , .

2:
. , , . - .

!

+3
2

, , ( , L, L ) , ... 2sqrt (n), O (Nsqrt (N)).

, , , sqrt (n), x, N/x. , 2sqrt (N).

, , 12, : 1,2,3 ( sqrt), 12/1, 12/2, 12/3 ( ).

2 , L , - N/L , O (N).

    static bool f(string s)
    {
        int n = s.Length;
        for (int l = n / 2; l >= 1; l--)
        {
            if (n % l != 0) continue;
            bool d = true;
            for (int o = 0; o < l; o++)
            {
                char f = s[o];
                for (int p = l; p < n; p += l)
                    if (s[p + o] != f) d = false;
            }
            if (d == true) return true;
        }
        return false;
    }

:

if (n % l != 0) continue;

O (N ^ 2).

N/2 , < 2sqrt (N). , - .

+2

, O (sqrt (n)), i, .

0

All Articles