guess the answer - problem solving - give it a try - show us your ideas

Discussion in 'Community Discussion' started by patent10021, Apr 28, 2019.

1. patent10021 macrumors 68030

Joined:
Apr 23, 2004
#1
Hospital A has 500 patient rooms, each numbered with a three-digit number beginning at 001. However, the digits 4 and 9 are not used anywhere in any room numbers. What is the room number for the 125th room?

A. 120
B. 166
C. 186
D. 202

2. Apr 29, 2019
Last edited: Apr 29, 2019

Huntn macrumors P6

Joined:
May 5, 2008
Location:
The Misty Mountains
#2
Is there a way to solve this without counting on you fingers? I guessed B, because for single digits, the number increases 2 for every 10 numbers, plus 20 for the 40s and 90s.

3. Apr 29, 2019
Last edited: Apr 29, 2019

Gutwrench Contributor

Joined:
Jan 2, 2011
#3

With detail as (

Select rrn(a) roomNumber
From tableName a
Where rrn(a) not like (‘%4%’, ‘%9%’)
Order by rrn(a) asc
Fetch first 125 rows only​

)

From detail a;

Instinct suggests there’s a trick by summing the digits in the answers will eliminate the wrong answers. But I’m more confident with my first solution.

4. Krayzkat Suspended

Joined:
Apr 22, 2011
#4
I counted in my head without looking at your answers and camera up with the number 167 lol, so i picked B as it nearer and i might have miscounted along the way.

Basically i counted how many 4's were used between 1 and 100. Which would be 10 for all the x4 numbers (ie 4, 34,64..), then i added another 9 to that to include all there 40's which were left (i had already counted 44 in previous calculation). That gave me 19, which i doubled to 38 as there should be the same amount of 9's as 4's. Then i added another 3 to the total for the 4's between 101 and 125, and another 2 for the 9's between 101-125. That gives 43 in total. I add 43 to 125 and get 167.

edit: i think you should be able to calculate this by converting 125 to a base 8 number. I can't remember how to do that right now, and i don't have a calculator handy

5. Apr 29, 2019
Last edited: Apr 30, 2019

NoBoMac macrumors 68020

Joined:
Jul 1, 2014
#5
186.

First 10 rooms use 12 numbers, second set of 10 need 13 (12+12 = 24, but can't have 4, so, add 1).
Third 10: 25+12 = 37
Fourth 10: 37+12= 49. Oops! 10 not possible in the scheme here (40-49), so add 10 plus 1 for 59. 60.

120/4 = 3 (how many groups of 40 room blocks)
60 * 3 = 180

5 more rooms to deal with, can't have 4, 5+1 + 180 = 186

Probably some clever formula out there for that, and something to do with squares.

ADD: Basically, every four "rooms" needs an increment of one. When at 40/90/140, extra 10 each needed for those (this might be formula-ed as well: (subtotal-rooms/40)*10)

So, 125/4 = 31.25 (31)
125+31 = 156
156/40 = 3.9 (3)
156 + (10*3) = 186

Room + (Room/4) = Delta_Room
((Delta_Room/40)*10) + Delta_Room = Room_Number

6. Krayzkat Suspended

Joined:
Apr 22, 2011
#6
ok it's definitely B/166, as i have figured out how it got 167 by mistake. I forgot that the number '49' doesn't need to be counted twice.

7. chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
betwixt
#7
Code:
```for N in {1..300}
do
echo \$N
done  \
| grep -v ''  \
| head -n 125 | tail
```
Output:
Code:
```175
176
177
178
180
181
182
183
185
186
```

8. Krayzkat Suspended

Joined:
Apr 22, 2011
#8
lol what does any of that mean

9. Apr 29, 2019
Last edited: Apr 29, 2019

chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
betwixt
#9
It's a sequence of bash commands that produces the output given. You can paste the first CODE section into a Terminal window and it should produce the output shown in the 2nd CODE section.

The last number in the output is the number that is assigned to the 125th room. The next-to-last number is what's assigned to the 124th room, etc.

If you want a more detailed description of what the commands are doing, just ask. It'd take me a while, but I'm willing to write it up.

On another note, if one googles the text of the question posed in post #1, it's interesting to see different people's answers and their explanations.

EDIT
Oh, 125 in octal (base 8) is 175. That's 1 * 64 + 7 * 8 + 5.

Also, recall that 128 in decimal would be 200 in octal, so subtract 3.
Remember to stay in octal, so 0200 - 1 would be 0177, not 199.

10. Apr 29, 2019
Last edited: Apr 29, 2019

Krayzkat Suspended

Joined:
Apr 22, 2011
#10
I have just realised that when i add the 43 to 125 to get 167, that is wrong because there are some numbers between 125 and 167 that contain 4's and 9's too.
So my new 'most definitely correct' answer is.....

- remove numbers containing 4 and 9 between range 125 to 167
- those will be 129 134 139 140 141 142 143 144 145 146 147 148 149 154 159 164
- that is 16 numbers
- add that to 167 to get 183
- check for any new 4's or 9's between 167 and 183
- there are 169 174 179 so that's 3
- add that to 183 to get 186
- check for any new 4's or 9's between 183 and 186
- there are 184 so that's 1
- add that to 183 to get 184
- erm
- dunno where the other 2 comes from to make that 186

edit: I used a online number generator to list all the numbers between 1 and 200. then i copy and pasted to a blank pages document. i then manually went through every number removing any containing a 4 or a 9. then i manually counted to the 125th number in the sequence. the answer is most definitely 186.
the 128th room would be numbered '200' btw

11. Gutwrench Contributor

Joined:
Jan 2, 2011
#11
Rain Man

12. Apr 29, 2019
Last edited: Apr 29, 2019

AngerDanger macrumors 601

Joined:
Dec 9, 2008
#12

Code:
```var currentRoom = 1, n = 0;
while (currentRoom <= 125){
n++;
if (n.toString().search(//) == -1)
currentRoom++;
}
console.log(n);
```
186

@chown33, very elegant solution. You ever heard of ProjectEuler? I think you'd get a kick out of that.

Joined:
Jan 2, 2011
14. NoBoMac macrumors 68020

Joined:
Jul 1, 2014
#14
Non-array version in Javascript (and basically C/C++, Java, Pascal, ALGOL, etc):

Code:
```var r_num = 1
var r_cnt = 1
var whichRoom = 125

while ( r_cnt <= whichRoom ) {
if ( r_num.toString().search(/(9|4)/) == -1 ) {
r_cnt++;
}
r_num++;
}
```
--- Post Merged, Apr 29, 2019 ---
I wanna see some LISP or FORTRAN (since we ARE playing with numbers).

15. chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
betwixt
#15
You've only posted a hypothetical SQL "ruler". Even a cat can claim to be a king.

If you don't show actual results and the commands to replicate them, it's not real.

16. Apr 29, 2019
Last edited: Apr 29, 2019

Gutwrench Contributor

Joined:
Jan 2, 2011
#16
With detail as (
Select rrn(a) roomNumber
From tableName a
Where rrn(a) not like (‘%4%’, ‘%9%’)
Order by rrn(a) asc
Fetch first 125 rows only

)
From detail a;

Answer = 186 I provided the actual query. Pretty elegant huh?

Substitute tableName for any table with >/= 186 rows.

17. chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
betwixt
#17
I recognized it as an actual query, but your original post didn't include initial conditions, such as creating a table with a suitable name, and an appropriate number of entries.

So suppose I start with no tables and a virgin run of the 'sqlite3' command that comes ready to use on a Mac. What's the complete sequence of commands, including the 'create table ...' etc.?

If this were submitted as homework, I'd have to give it an INCOMPLETE, or at best a C, because it's, well, incomplete.

It this were submitted as a unit test, I'd reject it because it doesn't specify initial conditions or prerequisites.

18. Apr 29, 2019
Last edited: Apr 29, 2019

Gutwrench Contributor

Joined:
Jan 2, 2011
#18
The query doesn’t need to create a table to execute, it uses a logical table named “detail”. “Detail” contains one column aptly named roomNumber and populated with the relative record numbers based on the where clause (excluding rrn’s containing 4,9). Then it selects the max roomNumber which is 186. It does, however, require the existence of any physical table (tableName) with >= 186 rows. I thought that assumption was declared. Please reconsider and give me an A or retest.

Ps - I probably could accomplish it with simply:

Select max(rrn(a)) roomNumber
From tableName a
Where rrn(a) not like (‘%4%’, ‘%9%’)
Order by rrn(a) asc
Fetch first 125 rows only;

But I’ve been too lazy to log in to confirm my result and syntax.

19. chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
betwixt
#19
I'll give you an extension to submit completed homework. Howzzat?

20. Gutwrench Contributor

Joined:
Jan 2, 2011
#20
Woot woot!

21. Apr 30, 2019
Last edited: Apr 30, 2019

Joined:
Apr 23, 2004
#21
Interesting... So why are you even bothering with numbers above 125?

Anyway, if anyone is interested, I worked out these two Swift versions. They both return 186.

Code:
```var roomNumber = 1, x = 0
repeat {
x += 1
if String(x).contains("4") || String(x).contains("9")  {
continue
}
roomNumber += 1
} while roomNumber <= 125
```
Code:
```var currentRoomNo = 1, n = 0
while currentRoomNo <= 125 {
n += 1
if String(n).contains("4") || String(n).contains("9")  {
continue
}
currentRoomNo += 1
}
```
I am still a bit confused though because when i count on paper, i.e. 4, 9, 14, 19, 24, 29.....40,41,42,43,44,45,46,47,48,49...84,89, 90,91,92,93,94,95,96,97,98,99...124, the total number of 4s and 9s is 45 but (125 + 45) obviously isn't the answer.

Also, I should tell you that the answer for this test was actually marked correct as 202. Someone said there were a total of 77 4s and 9s. 77 + 125 = 202. However, that answer seems to be unverified which is very strange. I like you, get 186. I wonder if there is something we are overlooking.

22. Krayzkat Suspended

Joined:
Apr 22, 2011
#22
I manually counted and the answer is definitely 186.

In regards to your total number of 4’s and 9’s between 1 and 125, you can’t just add that number to 125 because there are 4’s and 9’s that would get counted like 129, 134, etc

23. Gutwrench Contributor

Joined:
Jan 2, 2011
#23

Because numbers containing 4 & 9 exist between 1 and 125. Those must be excluded. Therefore you must go to the number 186 to reach a collection of 125 numbers that do not contain 4 & 9.

Another way to think of it. Count the first 125 prime numbers. The answer is 691.

24. NoBoMac macrumors 68020

Joined:
Jul 1, 2014
#24
Think what they did was count 36 4/9 to exclude per 100, so, came to some logical conclusion 36*2+5 for 101-125. And then did simple addition to 125.

Guessing some of the wrong answers might have also tried working 500 into the equation (red herring number).

A simple way to also verify: spreadsheet. Column with 1-200, delete the rows with 4/9, what is in row 125?

25. chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
betwixt
#25
I read about it years ago. I've replied to some threads in the Mac Programming forum that involve Project Euler questions. I just searched there for "Euler" and found a couple dozen posts, but not many with my replies. I did recognize some of the Euler threads, and can see why I didn't reply. I can also see why I did reply in some threads mentioning "Euler".

If I were a beginning programmer, or needed to hone my skills, then Project Euler problems would likely be useful. The two reasons I say this are:
1. The problems are clearly stated.
2. It's easy to find other's solutions.

The thing is, I'm not a beginner: see my numerous posts in Mac Programming. I apply my programming skills almost every day, to solve actual problems in the project I'm working on. If I want a diversion, solving more programming problems isn't really that appealing, since the result has no use for me, nor is the entertainment factor really present. For me, it's not entertaining if it's exactly like work. If I'm going to do something exactly like work, I'd rather do actual work instead of artificial work.

If I were learning a completely new language, then Project Euler might be useful to me. Then again, I already have a whole lot of other programs I've written, so I'd probably just rewrite one of them in the new language. I already have the source, and can see the expected output, so starting from scratch with just a problem statement might not be as useful as doing a conversion.