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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:00:15+00:00 2026-05-26T02:00:15+00:00

I don’t really understand how one uses proof by induction on psuedocode. It doesn’t

  • 0

I don’t really understand how one uses proof by induction on psuedocode. It doesn’t seem to work the same way as using it on mathematical equations.

I’m trying to count the number of integers that are divisible by k in an array.

Algorithm: divisibleByK (a, k)
Input: array a of n size, number to be divisible by k
Output: number of numbers divisible by k

int count = 0;
for i <- 0 to n do
    if (check(a[i],k) = true)
        count = count + 1
return count;


Algorithm: Check (a[i], k)
Input: specific number in array a,  number to be divisible by k
Output: boolean of true or false

if(a[i] % k == 0) then
    return true;
else    
    return false;

How would one prove that this is correct? 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-26T02:00:16+00:00Added an answer on May 26, 2026 at 2:00 am

    In this case I would interpret “inductively” as “induction over the number of iterations”.

    To do this we first establish a so called loop-invariant. In this case the loop invariant is:

                 count stores the number of numbers divisible by k with index lower than i.

    This invariant holds upon loop-entry, and ensures that after the loop (when i = n) count holds the number of values divisible by k in whole array.

    The induction looks like this:

    1. Base case: The loop invariant holds upon loop entry (after 0 iterations)

      Since i equals 0, no elements have index lower than i. Therefore no elements with index less than i are divisible by k. Thus, since count equals 0 the invariant holds.

    2. Induction hypothesis: We assume that the invariant holds at the top of the loop.

    3. Inductive step: We show that the invariant holds at the bottom of the loop body.

      After the body has been executed, i has been incremented by one. For the loop invariant to hold at the end of the loop, count must have been adjusted accordingly.

      Since there is now one more element (a[i]) which has an index less than (the new) i, count should have been incremented by one if (and only if) a[i] is divisible by k, which is precisely what the if-statement ensures.

      Thus the loop invariant holds also after the body has been executed.

    Qed.


    In Hoare-logic it’s proved (formally) like this (rewriting it as a while-loop for clarity):

    { I }
    { I[0 / i] }
    i = 0
    { I }
    while (i < n)
        { I ∧ i < n }
        if (check(a[i], k) = true)
            { I[i + 1 / i] ∧ check(a[i], k) = true }
            { I[i + 1 / i][count + 1 / count] }
            count = count + 1
            { I[i + 1 / i] }
        { I[i + 1 / i] }
        i = i + 1
        { I }
    { I ∧ i ≮ n }
    { count = ∑ 0 x < n;  1 if a[x] ∣ k, 0 otherwise. }
    

    Where I (the invariant) is:

         count = ∑x < i 1 if a[x]∣k, 0 otherwise.

    (For any two consecutive assertion lines ({...}) there is a proof-obligation (first assertion must imply the next) which I leave as an exercise for the reader 😉

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

Sidebar

Related Questions

I don't understand why this code doesn't work: function Messages(type,text) { console.log(In function Message);
Don't know a whole lot about streams. Why does the first version work using
Don't let below code scare you away . The question is really simple, only
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
I don't understand where the extra bits are coming from in this article about
Don't think that I'm mad, I understand how php works! That being said. I
I don’t think I’ve grokked currying yet. I understand what it does, and how
Don't these two mean the same thing, first get the value and then increment?
Don't ask why, but is there any way to suppress a failed linking error?

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.