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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:20:28+00:00 2026-05-14T08:20:28+00:00

I was thinking back to my freshman year at college (five years ago) when

  • 0

I was thinking back to my freshman year at college (five years ago) when I took an exam to place-out of intro-level computer science. There was a question about loop invariants, and I was wondering if loop invariants are really necessary in this case or if the question was simply a bad example… the question was to write an iterative definition for a factorial function, and then to prove that the function was correct.

The code that I provided for the factorial function was as follows:

public static int factorial(int x)
{
     if ( x < 0 ){
         throw new IllegalArgumentException("Parameter must be >= 0");
     }else if ( x == 0 ){
         return 1;
     }else{
         int result = 1;
         for ( int i = 1; i <= x; i++ ){
             result*=i;
         }
         return result;
     }
}

My own proof of correctness was a proof by cases, and in each I asserted that it was correct by definition (x! is undefined for negative values, 0! is 1, and x! is 1*2*3…*x for a positive value of x). The professor wanted me to prove the loop using a loop invariant; however, my argument was that it was correct “by definition”, because the definition of “x!” for a positive integer x is “the product of the integers from 1… x”, and the for-loop in the else clause is simply a literal translation of this definition. Is a loop invariant really needed as a proof of correctness in this case? How complicated must a loop be before a loop invariant (and proper initialization and termination conditions) become necessary for a proof of correctness?

Additionally, I was wondering… how often are such formal proofs used in the industry? I have found that about half of my courses are very theoretical and proof-heavy and about half are very implementation and coding-heavy, without any formal or theoretical material. How much do these overlap in practice? If you do use proofs in the industry, when do you apply them (always, only if it’s complicated, rarely, never)?

Edit
If we, ourselves, are convinced that a piece of code is correct, can convince others (informally) that it is correct, and there are unit tests in place, to what extent are formal proofs of correctness needed?

  • 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-14T08:20:28+00:00Added an answer on May 14, 2026 at 8:20 am

    to what extent are formal proofs of
    correctness needed?

    It depends, of course, but I think it’s important for programmers to know how to write code that is not prone to errors, where it tends to be correct by construction.

    One example is the concept of “look-ahead”, such as in parsing, where the next token of input is not “read”, then “looked at”, and then possibly “put back” if it is not what is wanted, but rather “looked at” and then possibly “accepted” if it is what is wanted. When, for example, writing loops to cycle through database records and extracting subtotals, this simple change in perspective can result in much simpler and more reliable code.

    Another example is differential execution, a technique I stumbled on many years ago. It appears to allow any algorithm to be incrementally re-executed, so as to incrementally update its results. I use it extensively in user interfaces where the contents can dynamically change. For a long time, I felt that it worked in all cases, but couldn’t be sure, until I finally proved it, as at the bottom of my Wikipedia page. After that, I knew that if I stuck to some simple constraints, I could rely on it to work, no matter how much code depended on it.

    At the same time, we may have utmost confidence in the correctness of some algorithm, but find it very difficult to formally prove, because our proof techniques are poor. Consider the lowly bubble-sort. It obviously works, but try to prove it formally, by applying rules to source code. I’ve done it, but it is not easy. I haven’t tried more advanced sorting algorithms.

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

Sidebar

Related Questions

A few years back I was thinking about ways to make a program that
Today I was thinking about a Python project I wrote about a year back
So recently i was thinking about strcpy and back to K&R where they show
I am getting back again and again to it thinking about the best way
Back in the days :) it was possible to find out which links where
I finally got back to fleshing out a GitCommit message mode that I want
When my laptop wiped a few months back, I had to re-install, and thinking
I'm sort of thinking out loud here, so let me know if I need
I was just thinking back to the good old days when I used to
My website URL: http://piquantblog.com Thinking it wouldn't come back to haunt me, I gave

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.