How to resolve the algorithm Babbage problem step by step in the C programming language
How to resolve the algorithm Babbage problem step by step in the C programming language
Table of Contents
Problem Statement
Charles Babbage, looking ahead to the sorts of problems his Analytical Engine would be able to solve, gave this example: He thought the answer might be 99,736, whose square is 9,947,269,696; but he couldn't be certain.
The task is to find out if Babbage had the right answer — and to do so, as far as your language allows it, in code that Babbage himself would have been able to read and understand. As Babbage evidently solved the task with pencil and paper, a similar efficient solution is preferred. For these purposes, Charles Babbage may be taken to be an intelligent person, familiar with mathematics and with the idea of a computer; he has written the first drafts of simple computer programmes in tabular form. [Babbage Archive Series L].
The aim of the task is to write a program that is sufficiently clear and well-documented for such a person to be able to read it and be confident that it does indeed solve the specified problem.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Babbage problem step by step in the C programming language
The code you provided is the implementation of the Babbage Problem in C programming language.
The Babbage Problem is a problem in mathematics that asks for the smallest number whose square ends in a given string of digits. In this case, the given string of digits is 269696.
The code starts by initializing two variables, current
and square
, to 0. Then, it enters a while loop that continues as long as the following two conditions are met:
- the square of
current
does not end in 269696 current
is less than the maximum possible integer value
Inside the loop, current
is incremented by 1 and the square of current
is calculated and stored in square
.
If the loop terminates because current
is equal to the maximum possible integer value, then the code prints a message saying that the condition was not satisfied before the maximum integer value was reached. Otherwise, the code prints a message saying that the smallest number whose square ends in 269696 is current
.
Here is an example of how the code would work:
current = 0
square = 0
square = current * current
square = 0
current = 1
square = current * current
square = 1
current = 2
square = current * current
square = 4
current = 3
square = current * current
square = 9
current = 4
square = current * current
square = 16
current = 5
square = current * current
square = 25
current = 6
square = current * current
square = 36
current = 7
square = current * current
square = 49
The loop continues in this manner until square
ends in 269696. In this case, current
will be equal to 7, and the code will print the following message:
The smallest number whose square ends in 269696 is 7
Source code in the c programming language
// This code is the implementation of Babbage Problem
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
int current = 0, //the current number
square; //the square of the current number
//the strategy of take the rest of division by 1e06 is
//to take the a number how 6 last digits are 269696
while (((square=current*current) % 1000000 != 269696) && (square<INT_MAX)) {
current++;
}
//output
if (square>+INT_MAX)
printf("Condition not satisfied before INT_MAX reached.");
else
printf ("The smallest number whose square ends in 269696 is %d\n", current);
//the end
return 0 ;
}
You may also check:How to resolve the algorithm Generator/Exponential step by step in the C++ programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the YAMLScript programming language
You may also check:How to resolve the algorithm Write language name in 3D ASCII step by step in the Perl programming language
You may also check:How to resolve the algorithm Map range step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Vector step by step in the ALGOL 68 programming language