4

This problem is easy to solve logically - try it for fun if you want before you run the code and see the answer.

Last@Select[IntegerName[Range[10^3],"Words"],!StringContainsQ[#,"n"]&]

Is there a shorter simpler version of this code?

Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355

2 Answers2

2

I know this isn't exactly fully automated code because I use some of my own logic at the beginning to narrow down the search space, but we don't have to search all the way to 1000. We only have to look at the integer names of 1-9, (and realize any integer $\geq~$ 100 has to have an "n")

Since "hundred has an "n" in it, we immediately know our number has to be less than 100 mod 1000, leading to an initial upper bound of 99:

upperBound = 99;

Note also this is an absolute upper bound. There is no integer greater than 99 that has no "n" in it because thosand, million, ..., $k-$illion all have "n" in them.

From here I thought of two similar methods:

Method 1

Outside of twenty, multipyling a digit 1-9 by 10 (ten,twenty,...,ninety) never adds an "n" to an integer name (there can be an "n" in the name, but it will be there because the 1-9 digit also has an "n"). For example thirty has no "n", and neither does three . Ninety has an "n" because nine has an "n".

So, the largest number with no "n" has to be largest number 1-9 with no n times ten, plus that number, or in other words, 11 times the largest 1-9 digit with no n:

largestNoNDig = 
  Select[Range[9], ! StringContainsQ[IntegerName@#, "n"] &] // Max;
11*largestNoNDig
(*88*)

This is the largest natural number with no "n" when written out using IntegerName.

Method 2

This method is kind of nice because we only have to check the IntegerNames of 4 numbers (90,80, 89, 88) to get the answer.

Since integer names 1-99 are just concatenation of the tens and ones number, we can lower our upper bound by checking if the tens digit has an "n". We will keep going down 10 units until we hit a tens digit without an "n"

upperBound = 99;
tensDig = upperBound - Mod[upperBound, 10];
While[StringContainsQ[IntegerName@tensDig, "n"], tensDig -= 10]
upperBound = tensDig + 9
(*89*)

And now we iterate until we hit a number with no "n" in it:

While[StringContainsQ[IntegerName@upperBound, "n", upperBound--]]
upperBound
(*88*)
ydd
  • 3,673
  • 1
  • 5
  • 17
2
Last @ Select[StringFreeQ @ "n"] @ IntegerName[Range[10^3], "Words"]
"eighty‐eight"
kglr
  • 394,356
  • 18
  • 477
  • 896