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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:12:07+00:00 2026-05-30T22:12:07+00:00

I wrote my own trivial little function (php for convenience) and was hoping someone

  • 0

I wrote my own trivial little function (php for convenience) and was hoping someone could help structure a proof by induction for it, just so I can get a very basic hang of it.

function add_numbers($max) {
  //assume max >= 2
  $index=1;
  $array=array(0);
  while ($index != $max) {
     //invariant: ∀ k:1 .. index-1, array[k]=array[k-1]+1
     $array[$index] = $array[$index-1]+1;
     $index += 1;
  }
}

The result being that the value at each index is the same as the index itself, though only because a[0] was initialized to 0.

I believe the goal is (or should be) to prove that the invariant (which may itself be suspect, but hopefully gets the point across) holds for k+1.

Thanks

edit: examples: http://homepages.ius.edu/rwisman/C455/html/notes/Chapter2/LoopInvariantProof.htm

  • 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-30T22:12:08+00:00Added an answer on May 30, 2026 at 10:12 pm

    Something like this, maybe, although this is a little pedantic.

    Invariant: when index = n, for n >= 1 (at the top of the loop where it checks the condition), array[i] = i for 0 <= i < n.

    Proof: The proof is by induction. In the base case n = 1, the loop is checking the condition for the first time, the body has not executed, and we have an outside guarantee that array[0] = 0, from earlier in the code. Assume the invariant holds for all n up to k. For k + 1, we assign array[k] = array[k-1] + 1. From the induction hypothesis, array[k-1] = k-1, so the value assigned array[k] is (k-1)+1 = k. Thus the invariant holds for the next, and by induction every, value of n (at the top of the loop).

    EDIT:

    function add_numbers($max) {
      //assume max >= 2
      $index=1;
      $array=array(63);
      while ($index != $max) {
         //invariant: ∀ k:1 .. index-1, array[k]=array[k-1]+1
         $array[$index] = $array[$index-1]+1;
         $index += 1;
      }
    }
    

    Invariant: when index = n, for n >= 1 (at the top of the loop where it checks the condition), array[i] = i + 63 for 0 <= i < n.

    Proof: The proof is by induction. In the base case n = 1, the loop is checking the condition for the first time, the body has not executed, and we have an outside guarantee that array[0] = 63, from earlier in the code. Assume the invariant holds for all n up to k. For k + 1, we assign array[k] = array[k-1] + 1. From the induction hypothesis, array[k-1] = (k-1) + 63 = k + 62, so the value assigned array[k] is (k+62)+1 = k+63. Thus the invariant holds for the next, and by induction every, value of n (at the top of the loop).

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

Sidebar

Related Questions

I wrote my own function that keeps a sidebar fixed on screen after a
I wrote my own Magento function which calculates the percentage relation of orders to
I wrote my own mvc for php, and it seems to be working fine
I wrote my own php captcha script, with a jpeg header, so that I
Usually I prefer to write my own solutions for trivial problems because generally plugins
I wrote my own custom namespace for a Spring based project I'm working on.
Ok I wrote my own version of SALT I call it salty lol don't
Ok, I admit it - I wrote my own view state facility for ASP.NET
With the upcoming rise of AJAX applications on the web, I wrote some own
Since I'm using Postgresql and can't use LINQ to SQL, I wrote my own

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.