Get a Jumpstart with our Sample Apps
Results 1 to 5 of 5

Thread: Programming Puzzle 27 - So, what's the page count?

  1. #1
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,298

    Default Programming Puzzle 27 - So, what's the page count?

    Puzzle 27

    It takes 1629 digits to number the pages of a book. How many pages does the book have?

    You may assume the first page is numbered "1".

    Write an xbasic script to solve the puzzle. You may display your result in a message box on screen, or you may write the result to the trace window.

    Have fun !



    Credit: Basic-mathematics.com for the puzzle.

  2. #2
    Moderator
    Real Name
    Steve Workings
    Join Date
    Apr 2000
    Location
    The Dreaded Chair
    Posts
    5,507

    Default Re: Programming Puzzle 27 - So, what's the page count?

    No takers? I'm sure someone will find a more efficient way, but this works:

    Code:
    dim charct as n = 0
    dim pageno as n = 0
    while 1=1
    	pageno = pageno + 1
    	charct = charct + len("" + pageno)
    	if charct >= 1629
    		exit while
    	end if
    end while
    msgbox("Number of pages in the book", "" + pageno)
    -Steve


  3. #3
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,298

    Default Re: Programming Puzzle 27 - So, what's the page count?

    Steve, that's right!

    I showed your solution to the "professor" and he was impressed. While mumbling about students who do well even though they "hang out" at the back of the class, he found two aspects of your solution noteworthy.

    a) The key to this puzzle is controlling the while .. end while code block. Your solution begins a loop that will never end because 1 is always equal to 1, but then, cleverly, exits when the desired condition is observed. A neat trick, that will be useful to many.

    b) To determine the number of characters in a numeric page number one must convert the numeric value to a character string. Many would use the STR() function, which results in leading blank spaces that must be trimmed before the length is determined. Your use the concatenation operator ( "+" ) neatly bypasses that complexity. By concatenating the null string with the page number, the operator will automatically convert the numeric to character data, and does so WITHOUT prepending blank spaces a la STR(). Readers may want to consult the docs, here, for details.

    Move to the head of the class.

  4. #4
    "Certified" Alphaholic
    Real Name
    Andrew Schone
    Join Date
    Dec 2005
    Location
    Kansas
    Posts
    1,044

    Default Re: Programming Puzzle 27 - So, what's the page count?

    /cough 3rd time is the charm

    ( course that is if I ignore attempts 2-5 )
    Attached Files Attached Files
    Andrew

  5. #5
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,298

    Default Re: Programming Puzzle 27 - So, what's the page count?

    Andy,

    The "professor" appreciates your tenacity. He agrees, "if at first you don't succeed, try, try again." And, this time, you got it.

    Nice use of the convert_type() function to convert your "numeric" page number into a "character" string so that its length (number of digits) could be determined.

    Good work.

Similar Threads

  1. Programming Puzzle 23 - For / Next Loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 9
    Last Post: 07-18-2012, 07:54 PM
  2. Programming Puzzle 22 - What do you Mean?
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 5
    Last Post: 05-23-2012, 08:09 AM
  3. Programming Puzzle 3 - Fractions, anyone?
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 7
    Last Post: 11-08-2011, 06:40 AM
  4. Programming Puzzle 21 - Got ini ?
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 5
    Last Post: 09-08-2011, 10:19 PM
  5. Programming Puzzle 11 - More Loops
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 11
    Last Post: 08-20-2011, 05:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •