patterncsharpMinor
Squaring the two digits in the middle of a four-digit number
Viewed 0 times
thenumberdigitsfourtwodigitmiddlesquaring
Problem
I got a four-digit positive number. I need to square the two digits in middle to generate a new number on which the same procedure is applied. The numbers get saved in a list. This loops as long as there isn't an equal number generated (the list contains the number already). If a number is generated containing less than four digits, the code takes the first and middle digit.
I've done the core magic within
- 1 23 4
- 23² = 529
- 52 9
- 52² = 2704
- 2 70 4
- 70² = ...
I've done the core magic within
int current = (int) Math.Pow (number % 1000 / 10, 2);. Is this the most elegant way to solve this?ArrayList numbers = new ArrayList ();
public void generateNumbers (int number) {
// just in case :)
if (number > 9999) {
throw new ArgumentOutOfRangeException ("Number must be smaller than 10,000");
}
// Remove the thousands and get the times 10 fits in (you've only the two numbers in the middle left)
int current = (int) Math.Pow (number % 1000 / 10, 2);
// Repeat until an equal number is in the ArrayList
if (numbers.Contains (current)) {
return;
} else {
numbers.Add (current);
this.generateNumbers (current);
}
}Solution
Some thoughts:
1) I would add a check to ensure that the number being passed in, is positive.
2) You don't need to use the return statement in your first if, in fact the first if does nothing. You could simply add a
3) If you decided to go with a larger number and square using more digits you could potentially run into a StackOverflowException since this uses recursion. I would use a helper method to do the actual calculation which would return a number and instead of an if statement use a while. (Of course if you're checking a cap on the number this could never happen since there can only be 100 numbers that will be added.)
How the code could look:
Now (with a bit of editing) you could extend the method to take as high of a number as the person wants and always square whats in the middle.
1) I would add a check to ensure that the number being passed in, is positive.
2) You don't need to use the return statement in your first if, in fact the first if does nothing. You could simply add a
! to the if statement and if it passes over it the method will terminate anyway.// Repeat until an equal number is in the ArrayList
if (!numbers.Contains(current)){
numbers.Add (current);
this.generateNumbers(current);
}3) If you decided to go with a larger number and square using more digits you could potentially run into a StackOverflowException since this uses recursion. I would use a helper method to do the actual calculation which would return a number and instead of an if statement use a while. (Of course if you're checking a cap on the number this could never happen since there can only be 100 numbers that will be added.)
How the code could look:
ArrayList numbers = new ArrayList ();
public void generateNumbers (int number) {
if (number 9999) {
throw new ArgumentOutOfRangeException ("Number must be greater than 0 and less than 10,000");
}
// Repeat until an equal number is in the ArrayList
do {
number = helper(number);
numbers.Add(number);
} while (!numbers.Contains(number))
}
private int helper(int number) {
// Remove the thousands and get the times 10 fits in (you've only the two numbers in the middle left)
return (int) Math.Pow(number % 1000 / 10, 2);
}Now (with a bit of editing) you could extend the method to take as high of a number as the person wants and always square whats in the middle.
Code Snippets
// Repeat until an equal number is in the ArrayList
if (!numbers.Contains(current)){
numbers.Add (current);
this.generateNumbers(current);
}ArrayList<int> numbers = new ArrayList<int> ();
public void generateNumbers (int number) {
if (number < 0 || number > 9999) {
throw new ArgumentOutOfRangeException ("Number must be greater than 0 and less than 10,000");
}
// Repeat until an equal number is in the ArrayList
do {
number = helper(number);
numbers.Add(number);
} while (!numbers.Contains(number))
}
private int helper(int number) {
// Remove the thousands and get the times 10 fits in (you've only the two numbers in the middle left)
return (int) Math.Pow(number % 1000 / 10, 2);
}Context
StackExchange Code Review Q#138360, answer score: 7
Revisions (0)
No revisions yet.