I'll start by stating what I think your intent is, as it's not 100% clear from your question.
Imagine the series that consists of all positive natural numbers without multiples of 10 : ℕ \10ℕ, in order. Write a function that, given a 0-based index, returns the number at that index in the series
This is what seems to correspond most, with what you have described and what your code does. However there is a discrepancy between what you say your code does, and what it actually does. According to the semantics 10 should map to 12, and this is what your code does, yet as an example you say 10 should map to 11.
The code you have does not seem to fulfill its contract (i.e. it has a semantical bug) it starts misbehaving for iputs higher than 198. 198 gives 221 and 199 also gives 221.
Or, of course, I am way off track...
Code that does fulfill the contract I describe above is actually fairly simple.
public static int getStrippedNumber(int num) {
return num + num/9 + 1;
}
Borrowing from @tintinmj I have also submitted a ideone to demonstrate
Of course naming this function more appropriately and having a clearer contract explanation are major points of attention. Imagine a developer trying to maintain the code, and all he has is your code and what you have documented.
So improved this would be :
/**
* Determines the number in the series ℕ\10ℕ (the natural numbers without multiples of 10) at the given index.
* @param index zero-based index
* @return the number in the series of ℕ\10ℕ at the given index.
*/
public static int getNwithout10NAtIndex(int index) {
if (index < 0) {
throw new IllegalArgumentException();
}
return index + index/9 + 1;
}
19 -> 21
? What is the expected output for100
and998
and why? \$\endgroup\$0 -> 2
,10 -> 14
, and19 -> 23
. You should post here once you have working code so we can help you improve it. Non-working code is for Stack Overflow. \$\endgroup\$0
in19
then why it increases to22
? \$\endgroup\$