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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:33:20+00:00 2026-05-17T19:33:20+00:00

I have a for loop which gives a given integer sequence, for fixed parameters

  • 0

I have a for loop which gives a given integer sequence, for fixed parameters N and D :

    int i = 0, j = 0;
    for (int k=0; k<N; k++) {             
      sequence[k] = i;
      if ((i += D) >= N) i = ++j;
    }

I’d like to find a simple formula which reproduces this sequence, depending only on N and D (and the index k), like sequence[k] = D*(k%D)+ k/D (which doesn’t work). I tried hard but I just can’t find something which always work for any value of N and D!

Thanks!

  • 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-17T19:33:21+00:00Added an answer on May 17, 2026 at 7:33 pm

    Here’s the formula. At the moment it requires a conditional statement, but you can always express it via a function returning 0 or 1 if you want pure functional form.

    I wrote it as Perl function, to ease testing (I tested for all N<=20 and D between 0 and N)

    sub div { my ($x, $y) = @_; return ($x-$x%$y)/$y }; # whole division
    
    my $small_subsequence_length = div($N, $D);
    my $big_subsequence_length = $small_subsequence_length + 1;
    my $num_big_subseqiences = $N % $D;
    my $num_total_big_subsequence_numbers = $big_subsequence_length * $num_big_subseqiences;
    my $num_total_small_subsequence_numbers = $N - $num_total_small_subsequence_numbers;
    my $num_small_subseqiences = div($num_total_small_subsequence_numbers, $small_subsequence_length);
    
    sub sequence {
        my $k = $_[0];
        my ($subsequence_num, subsequence_offset);
    
        if ($k > $num_total_big_subsequence_numbers) {
            my $k2 = $k - $num_total_big_subsequence_numbers;
            $subsequence_num = div($k2, $small_subsequence_length) + $num_big_subseqiences;
            $subsequence_offset = ($k2 % $small_subsequence_length) * $D;
        } else {
            $subsequence_num = div($k, $big_subsequence_length);
            $subsequence_offset = ($k % $big_subsequence_length) * $D;
        }
        return $subsequence_offset + $subsequence_num;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a for loop which is nested within another, how can I
I have some code which collects points (consed integers) from a loop which looks
I have the following command which will loop over all the subdirectories in a
In Java 5 and above you have the foreach loop, which works magically on
I have two SQL scripts which get called within a loop that accept a
I have a loop that reads each line in a file using getline() :
I have a loop on page to update an access database that takes 15-20
In my WPF client, I have a loop that calls a WCF service to
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
If you have a for loop such as: For Each s As String In

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.