Author Topic: Killer Counting Problem from Ashish  (Read 1789 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Killer Counting Problem from Ashish
« on: June 03, 2020, 12:06:33 am »
Ashish:
Quote
Do you really want to play with numbers.... I have a puzzle for you then
Create a 10 digit number so that the first digit is the number of zeros in the number, the second digit is the number of ones in the number, the third digit is the number of twos, etc.

Here is my count(s):
Code: QB64: [Select]
  1. _TITLE "Killer Counting Problem from Ashish" 'b+ 2020-06-03
  2. DIM d(9)
  3. s$ = STRING$(3, "5") + STRING$(7, "0") '<<< doesn't matter where you start even a string too long or short,  keep counts of digits under 10 though
  4. restart:
  5. FOR i = 1 TO LEN(s$)
  6.     m = VAL(MID$(s$, i, 1))
  7.     d(m) = d(m) + 1
  8. n$ = ""
  9. FOR i = 0 TO 9
  10.     n$ = n$ + _TRIM$(STR$(d(i)))
  11. s$ = n$
  12. INPUT "OK press enter to continue, any else to quit "; continue$
  13. IF LEN(continue$) = 0 THEN GOTO restart
  14.  
  15.  

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Killer Counting Problem from Ashish
« Reply #1 on: June 03, 2020, 12:27:06 am »
Is this program giving the possible solution? If yes, I do not find the correct one in your program.
BTW, qbkiller have already told me the answer at discord. He did that by writing a script in JavaScript.

One side note, This problem is not created by me or its made by me. The original problem is here - http://paulbourke.net/fun/zeros.html

Also, I was trying to solve this problem without using any computer program , etc but since qbkiller had told me the answer, I have lost
all the interest in this very good puzzle.
« Last Edit: June 03, 2020, 12:29:27 am by Ashish »
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Killer Counting Problem from Ashish
« Reply #2 on: June 03, 2020, 12:54:06 am »
By hand I started with

0000000000

So maybe

9000000000

but then

8100000000

but finally

6210001000

worked.
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Killer Counting Problem from Ashish
« Reply #3 on: June 03, 2020, 01:02:56 am »
Is this program giving the possible solution? If yes, I do not find the correct one in your program.
BTW, qbkiller have already told me the answer at discord. He did that by writing a script in JavaScript.

One side note, This problem is not created by me or its made by me. The original problem is here - http://paulbourke.net/fun/zeros.html

Also, I was trying to solve this problem without using any computer program , etc but since qbkiller had told me the answer, I have lost
all the interest in this very good puzzle.

Oh looks like STxAxTIC has a solution but is it the only one? LOL

@Ashish Not posted as solution, I didn't think there might be one. I thought if there were one then the code would get to it, sort of like a limit, but alas it didn't. Looks like it was close. I was impressed that any string of digits kept going to that sequence of two numbers after say 5 iterations.

Quote
BTW, qbkiller have already told me the answer at discord. He did that by writing a script in JavaScript.
@qbkiller101  I'd love to see it! :)
« Last Edit: June 03, 2020, 01:08:36 am by bplus »

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Killer Counting Problem from Ashish
« Reply #4 on: June 03, 2020, 01:09:20 am »
Oh looks like STxAxTIC has a solution but is it the only one? LOL
Yeah, He has the solution, but I *do not* know whether there is multiple solutions or not.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Killer Counting Problem from Ashish
« Reply #5 on: June 03, 2020, 01:23:52 am »
Just curious, but isn't the value reversed??

123....   

First digit would be 3, Second digit 2, Third digit 1...

With numbers, don't we count digits from right to left usually?  Ones, tens, hundreds, thousands...

(Not that it's a big deal, as you just reverse the solution, but as it sits, I *never* would've got it right.  My 6210001000 would end up being 0001000126, just because I think right to left in numerical digits.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline qbkiller101

  • Newbie
  • Posts: 73
    • View Profile
Re: Killer Counting Problem from Ashish
« Reply #6 on: June 03, 2020, 01:34:47 am »
Oh looks like STxAxTIC has a solution but is it the only one? LOL

@Ashish Not posted as solution, I didn't think there might be one. I thought if there were one then the code would get to it, sort of like a limit, but alas it didn't. Looks like it was close. I was impressed that any string of digits kept going to that sequence of two numbers after say 5 iterations.
@qbkiller101  I'd love to see it! :)
@bplus  same that static told :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Killer Counting Problem from Ashish
« Reply #7 on: June 03, 2020, 11:43:04 am »
@bplus  same that static told :)

@qbkiller101 Yeah I assumed you got the answer but if you used a program that is different than just figuring from your head. It's the program I'd like to see. Is Java Script the first Programming Language you learned?

The digits do have to add up to 10, plug in permutations that add to 10 and using lowest digits?

I see Steve has found a 2nd solution, sorta :)
« Last Edit: June 03, 2020, 12:05:42 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Killer Counting Problem from Ashish
« Reply #8 on: June 03, 2020, 11:51:08 am »
@qbkiller101 Yeah I assumed you got the answer but if you used a program that is different that just figuring from your head. It's the program I'd like to see. Is Java Script the first Programming Language you learned?

The digits do have to add up to 10, plug in permutations that add to 10 and using lowest digits?

I see Steve has found a 2nd solution, sorta :)

Little Endian answer vs Big Endian.  It's the same value, just represented in reverse order.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Killer Counting Problem from Ashish
« Reply #9 on: June 03, 2020, 12:04:57 pm »
Little Endian answer vs Big Endian.  It's the same value, just represented in reverse order.  ;)

Yeah, the people in Cleveland are missing the Endians this summer.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Killer Counting Problem from Ashish
« Reply #10 on: June 03, 2020, 04:17:35 pm »
This code solves the problem 100 times in around 1/4 of a million Random Generated Solutions:
Code: QB64: [Select]
  1. _TITLE "Killer Counting Problem Rnd Solver" 'b+ 2020-06-03
  2. DIM d(9)
  3.  
  4. restart:
  5. iteration = iteration + 1
  6. s$ = makeRnd10$
  7. 's$ = "6210001000" 'check and make sure it recognizes a solve
  8. FOR i = 1 TO LEN(s$)
  9.     m = VAL(MID$(s$, i, 1))
  10.     d(m) = d(m) + 1
  11. n$ = ""
  12. FOR i = 0 TO 9
  13.     n$ = n$ + _TRIM$(STR$(d(i)))
  14.  
  15. PRINT _TRIM$(STR$(iteration)); "# "; "Rnd Sol'n: "; s$; " check: "; n$
  16. IF s$ = n$ THEN
  17.     solved = solved + 1
  18.     PRINT _TRIM$(STR$(iteration)); "# "; "solved! "; _TRIM$(STR$(solved)); " times now."
  19.     IF s$ <> "6210001000" THEN PRINT "Holy Mother of God a 2nd solution!!!!!": END
  20.     IF solved = 100 THEN END 'ELSE _DELAY 2
  21. 'INPUT "OK press enter to continue, any else to quit "; continue$
  22. IF LEN(continue$) = 0 THEN GOTO restart
  23.  
  24. FUNCTION makeRnd10$ ()
  25.     'generate a random solution keeping in mind:
  26.     ' 1. the digits have to add to 10
  27.     ' 2. the lower digits are way more likely than the higher digits 0's being most likely 9's least
  28.     DIM d(9)
  29.     FOR i = 0 TO 9
  30.         r = INT(RND * (11 - l))
  31.         WHILE r = 10
  32.             r = INT(RND * (11 - l))
  33.         WEND
  34.         l = l + r
  35.         'PRINT l;
  36.         d(i) = r
  37.     NEXT
  38.     PRINT
  39.     FOR i = 0 TO 9
  40.         rs$ = rs$ + _TRIM$(STR$(d(i)))
  41.     NEXT
  42.     makeRnd10$ = rs$
  43.  
  44.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Killer Counting Problem from Ashish
« Reply #11 on: June 03, 2020, 04:30:11 pm »
You know, Steve might have a point if you look for the lowest numbered solution.

In fact, what if we just counted up and tested if it met the conditions of a solution?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Killer Counting Problem from Ashish
« Reply #12 on: June 03, 2020, 07:30:16 pm »
Yeah, this proves there is only 1 soln and after an hour+ run. I edited code to bug out when found at 1000126.
Code: QB64: [Select]
  1. _TITLE "Killer Counting Problem, Solve by Counting Up" 'b+ 2020-06-03
  2. DIM solved$(10)
  3. DIM d(9)
  4. FOR n = 1 TO 1000000000 '9 0's and 1 1?
  5.     ' n = 1000126   'check soln stops
  6.     ERASE d: tot = 0
  7.     ns$ = RIGHT$(STRING$(10, "0") + _TRIM$(STR$(n)), 10)
  8.     IF n MOD 1000000 = 1 THEN PRINT n, ns$, "solns:"; soln
  9.  
  10.     FOR i = 1 TO LEN(ns$)
  11.         m = VAL(MID$(ns$, i, 1))
  12.         d(m) = d(m) + 1
  13.         tot = tot + m
  14.     NEXT
  15.     IF tot = 10 THEN
  16.         ck$ = ""
  17.         FOR i = 9 TO 0 STEP -1
  18.             ck$ = ck$ + _TRIM$(STR$(d(i)))
  19.         NEXT
  20.         IF ck$ = ns$ THEN
  21.             soln = soln + 1: solved$(soln) = ns$
  22.             EXIT FOR 'only 1 soln so bug out
  23.         END IF
  24.     END IF
  25.     'INPUT "OK "; cont$
  26. PRINT "All done!, # solutions found: "; soln 'OK there is only 1 soln so quit when have it
  27. FOR i = 1 TO soln
  28.     PRINT solved$(i)
  29.