Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Babbage-problem/C-sharp/babbage-problem.cs
Normal file
35
Task/Babbage-problem/C-sharp/babbage-problem.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
namespace Babbage_Problem
|
||||
{
|
||||
class iterateNumbers
|
||||
{
|
||||
public iterateNumbers()
|
||||
{
|
||||
long baseNumberSquared = 0; //the base number multiplied by itself
|
||||
long baseNumber = 0; //the number to be squared, this one will be iterated
|
||||
|
||||
do //this sets up the loop
|
||||
{
|
||||
baseNumber += 1; //add one to the base number
|
||||
baseNumberSquared = baseNumber * baseNumber; //multiply the base number by itself and store the value as baseNumberSquared
|
||||
}
|
||||
while (Right6Digits(baseNumberSquared) != 269696); //this will continue the loop until the right 6 digits of the base number squared are 269,696
|
||||
|
||||
Console.WriteLine("The smallest integer whose square ends in 269,696 is " + baseNumber);
|
||||
Console.WriteLine("The square is " + baseNumberSquared);
|
||||
|
||||
}
|
||||
|
||||
private long Right6Digits(long baseNumberSquared)
|
||||
{
|
||||
|
||||
string numberAsString = baseNumberSquared.ToString(); //this is converts the number to a different type so it can be cut up
|
||||
|
||||
if (numberAsString.Length < 6) { return baseNumberSquared; }; //if the number doesn't have 6 digits in it, just return it to try again.
|
||||
|
||||
numberAsString = numberAsString.Substring(numberAsString.Length - 6); //this extracts the last 6 digits from the number
|
||||
|
||||
return long.Parse(numberAsString); //return the last 6 digits of the number
|
||||
|
||||
}
|
||||
}
|
||||
}}
|
||||
Loading…
Add table
Add a link
Reference in a new issue