Post Reply 
challenge
01-19-2015, 03:06 PM
Post: #1
challenge
How many integers (from 1 to 9999) are evenly divisible by the number of letters in their name?

First one is 4 (divisible by f-o-u-r 4).

Last one happens to be 9999 (divisible by n-i-n-e-t-h-o-u-s-a-n-d-n-i-n-e-h-u-n-d-r-e-d-n-i-n-e-t-y-n-i-n-e 33).

For you HP50 fans, I expect to see a one-line RPL program!

If Valentin is lurking, he just knows the answer without writing a program.

I'll post a Tandy model 102 BASIC program in a couple of days.
Find all posts by this user
Quote this message in a reply
01-19-2015, 07:22 PM (This post was last modified: 01-19-2015 07:23 PM by Thomas Klemm.)
Post: #2
RE: challenge
I've started with this in Python:
Code:
units = ['','one','two','three','four','five','six','seven','eight','nine']
teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
tens  = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']

def number_to_text(t, h, z, e):
  if t + h + z + e == 0:    
    return 'zero'
  text =  units[t] + 'thousand' if t > 0 else ''
  text += units[h] + 'hundred'  if h > 0 else ''
  if z == 1:                     
    text += teens[e]             
  else:                          
    text += tens[z] + units[e]   
  return text                    

ten = range(10)

count = 0
for t in ten:
  for h in ten:
    for z in ten:
      for e in ten:
        text = number_to_text(t, h, z, e)
        length, n = len(text), 10*(10*(10*t + h) + z) + e
        if n % length == 0:
          # print n, text, length
          count += 1

print count

But as we are only interested in the number of letters we can use these instead:
Code:
units_len = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
teens_len = [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
tens_len  = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]

zero_len = 4
hundred_len = 7
thousand_len = 8

def length_of_number(t, h, z, e):
  if t + h + z + e == 0:    
    return zero_len
  length =  units_len[t] + thousand_len if t > 0 else 0
  length += units_len[h] + hundred_len  if h > 0 else 0
  if z == 1:                     
    length += teens_len[e]             
  else:                          
    length += tens_len[z] + units_len[e]   
  return length                    

ten = range(10)

count = 0
for t in ten:
  for h in ten:
    for z in ten:
      for e in ten:
        length, n = length_of_number(t, h, z, e), 10*(10*(10*t + h) + z) + e        
        if n % length == 0:
          # print n, length
          count += 1

print count

In my program zero is included thus you have to subtract 1 from the result.
Not in RPL and not a one-liner, I know.

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
01-19-2015, 09:00 PM
Post: #3
RE: challenge
So Thomas, what is your number?
Find all posts by this user
Quote this message in a reply
01-19-2015, 09:08 PM (This post was last modified: 01-19-2015 09:47 PM by Gilles.)
Post: #4
RE: challenge
RPL but not one-liner. Recursive approch.

Code:

DIR
 J20   { "one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen", "nineteen"}
 J100  { "twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}
 Tête  « →STR HEAD STR→ »
 Queue « →STR TAIL STR→ »

 →TXT
  « → n
   « 
    CASE
     'n==0'             THEN "" END
     'n<20'             THEN 'J20' n GET END
     'n≥20 AND n<100'   THEN 'J100'  n Tête 1 - GET n Queue →TXT END
     'n≥100 AND n≤999'  THEN n Tête →TXT "hundred"  n Queue →TXT END
     'n≥1000'           THEN n Tête →TXT "thousand" n Queue →TXT END
    END
   »
   +
  »
  
 Count
  « 
   0
   1 9999 FOR n n 1. DISP
    IF n "" n →TXT SIZE MOD NOT THEN 1 + DUP 2. DISP END
   NEXT
  »
END

Count returns 359 (without AND)

By the way, my english is bad but 9999 should it not be writen as : n-i-n-e-t-h-o-u-s-a-n-d-n-i-n-e-h-u-n-d-r-e-d-A-N-D-n-i-n-e-t-y-n-i-n-e
? (36 letters with an AND inside)
Find all posts by this user
Quote this message in a reply
01-19-2015, 09:12 PM
Post: #5
RE: challenge
359

The forum prevented me from just posting this number:
Quote:[Image: error.gif] Please correct the following errors before continuing:
  • The message is too short. Please enter a message longer than 5 characters.
Find all posts by this user
Quote this message in a reply
01-19-2015, 09:19 PM
Post: #6
RE: challenge
(01-19-2015 09:08 PM)Gilles Wrote:  "hundredand"

Our results probably won't agree. From the example 9999 I assumed not to use and after hundred. Maybe Don can clarify this?

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
01-19-2015, 09:37 PM
Post: #7
RE: challenge
(01-19-2015 09:19 PM)Thomas Klemm Wrote:  
(01-19-2015 09:08 PM)Gilles Wrote:  "hundredand"

Our results probably won't agree. From the example 9999 I assumed not to use and after hundred. Maybe Don can clarify this?

Cheers
Thomas

I changed my program. I get 359 also ;D
Find all posts by this user
Quote this message in a reply
01-19-2015, 10:24 PM
Post: #8
RE: challenge
(01-19-2015 09:19 PM)Thomas Klemm Wrote:  
(01-19-2015 09:08 PM)Gilles Wrote:  "hundredand"

Our results probably won't agree. From the example 9999 I assumed not to use and after hundred. Maybe Don can clarify this?

Cheers
Thomas

Yes, in America at least, the word "and" is reserved for the decimal point. In other words, 4278.16 would be "four thousand two hundred seventy eight AND 16 hundredths." That's what we teach the kids.

So, yes, 359, and if you include zero (which is evenly divisible by 4) in the tally, you get the model number of IBM's most famous and (probably) popular mainframe computer. Hmm, maybe that is the basis of how they named it.

Smile
Find all posts by this user
Quote this message in a reply
01-19-2015, 10:32 PM
Post: #9
RE: challenge
(01-19-2015 10:24 PM)Don Shepherd Wrote:  Yes, in America at least, the word "and" is reserved for the decimal point. In other words, 4278.16 would be "four thousand two hundred seventy eight AND 16 hundredths." That's what we teach the kids.

Perhaps, but it's not how most adults say it, at least in my experience. I think that "four thousand two hundred seventy eight POINT sixteen" or "four thousand two hundred seventy eight SPOT sixteen" or "four thousand two hundred seventy eight POINT (or SPOT or even DECIMAL) one six" is more much common. "SPOT" is very common among wall street-type people, not so much so with non-financial people.

-katie

Visit this user's website Find all posts by this user
Quote this message in a reply
01-19-2015, 10:52 PM
Post: #10
RE: challenge
(01-19-2015 10:32 PM)Katie Wasserman Wrote:  
(01-19-2015 10:24 PM)Don Shepherd Wrote:  Yes, in America at least, the word "and" is reserved for the decimal point. In other words, 4278.16 would be "four thousand two hundred seventy eight AND 16 hundredths." That's what we teach the kids.

Perhaps, but it's not how most adults say it, at least in my experience. I think that "four thousand two hundred seventy eight POINT sixteen" or "four thousand two hundred seventy eight SPOT sixteen" or "four thousand two hundred seventy eight POINT (or SPOT or even DECIMAL) one six" is more much common. "SPOT" is very common among wall street-type people, not so much so with non-financial people.

Wow, I never heard of SPOT before. But then again, I've never visited Wall Street. Yes, POINT is very common. I teach my students to reserve the word AND for the decimal point, but when they get out in the real world, they hear everything, like "four double naught seven" for 4007.

Here is some BASIC code for the Tandy 102, a nice little BASIC machine:
Code:

10 data 3,3,5,4,4,3,5,5,4 'one two three ... nine
20 for i=1to9:read oe(i):next
30 data 3,6,6,8,8,7,7,9,8,8 'ten eleven twelve ... nineteen
40 for i=1to10:read en(i):next
50 data 6,6,5,5,5,7,6,6 'twenty thirty forty ... ninety
60 for i=1to8:read tn(i):next
70 th=8: hu=7
80 for d4=0 to 9 'thousands digit
90 for d3=0 to 9 'hundreds digit
100 for d2=0 to 9 'tens digit
110 for d1=0 to 9 'ones digit
120 c=0 'num letters in this number
130 n=d4*1000+d3*100+d2*10+d1
140 if d4>0 then c=c+th+oe(d4)
150 if d3>0 then c=c+hu+oe(d3)
160 if d2>1 then c=c+tn(d2-1)
170 if d2=1 then c=c+en(d1+1):goto 190
180 if d1>0 then c=c+oe(d1)
190 if n mod c = 0 then a=a+1
200 next d1:next d2:next d3:next d4
210 print a
Find all posts by this user
Quote this message in a reply
01-19-2015, 10:55 PM (This post was last modified: 01-19-2015 11:07 PM by Gilles.)
Post: #11
RE: challenge
A shorter, faster but less understandable version :

Code:

« → n
 « 
  CASE
   'n==0.'  THEN 0. END
   'n<20.'  THEN {3.,3.,5.,4.,4.,3.,5.,5.,4.,3.,6.,6.,8.,8.,7.,7.,9.,8.,8.} n GET END
   'n<100.' THEN {6.,6.,5.,5.,5.,7.,6.,6.} n →STR HEAD STR→  1 - GET n →STR TAIL STR→  →TXT END
   'n≤999.' THEN n →STR HEAD STR→  →TXT 7. n →STR TAIL STR→ →TXT END
   n →STR HEAD STR→ →TXT 8. n →STR TAIL STR→ →TXT 
  END
 »
 +
»
'→TXT' STO

« 
 0.
 1. 9999. FOR n 
  n 0. n →TXT MOD NOT {1 +} IFT
 NEXT
»
'Count' STO
Find all posts by this user
Quote this message in a reply
01-20-2015, 06:10 PM (This post was last modified: 05-23-2016 06:59 PM by Hlib.)
Post: #12
RE: challenge
(01-19-2015 03:06 PM)Don Shepherd Wrote:  How many integers (from 1 to 9999) are evenly divisible by the number of letters in their name?
...For you HP50 fans, I expect to see a one-line RPL program!...

I understand your irony :-) For such simple tasks a BASIC is more comfortable. First I wrote this program on CASIO-BASIC for FX-2.0 within an hour, while my algorithm on RPL did not finished yet.
Code:

[[0,3,3,5,4,4,3,5,5,4][3,6,6,8,8,7,7,9,8,8][0,0,6,6,5,5,5,7,6,6]]→Mat K:-1→N:0→K:
For 1→E To 10:Mat K[1,E]+8(E≠1)→S:
  For 1→D To 10:Mat K[1,D]+7(D≠1)+S→T:
    For 1→C To 10:Mat K[3,C]+T→U:(C=2)+1→B:
      For 1→A To 10:Mat K[B,A]+U→V:Isz N:(Frac (N/V)=0)+K→K:
      Next:
    Next:
  Next:
Next:"K=":K

N=9999 K=359 time=350sec size=256b
Find all posts by this user
Quote this message in a reply
01-21-2015, 04:03 PM (This post was last modified: 01-21-2015 04:47 PM by Hlib.)
Post: #13
RE: challenge
I don`t see RPL fonders. Were are you? Whether <Tim Vessman> will be able to answer us something to show advantages of RPL or "banana-products" like HP-NEXT?
Find all posts by this user
Quote this message in a reply
01-21-2015, 04:14 PM
Post: #14
RE: challenge
(01-21-2015 04:03 PM)Hlib Wrote:  I don`t see RPL fonders. Were are you?

I think Giles entry above is an RPL program, although not a "one-liner".

I'm always amazed at what people can do with RPL, I just never wanted to take the time to learn it.
Find all posts by this user
Quote this message in a reply
01-21-2015, 09:10 PM
Post: #15
RE: challenge
A shorter RPL version :

Code:

« DUP ->STR -> n s
 « CASE
    n 0  == THEN 0 END
    n 20  < THEN "3354435543668877988" n DUP SUB STR-> END
    n 100 < THEN "66555766" s HEAD STR-> 1 - DUP SUB STR-> s TAIL STR-> Nbr END
    n 999 ≤ THEN s HEAD STR-> Nbr 7 s TAIL STR-> Nbr END
    s HEAD STR-> Nbr 8 s TAIL STR-> Nbr
   END »
   +
» 'Nbr' STO

« 0 1 9999 FOR n n 0 n Nbr MOD NOT {1 +} IFT NEXT » 'Count' STO
Find all posts by this user
Quote this message in a reply
01-21-2015, 09:32 PM (This post was last modified: 01-21-2015 09:38 PM by Don Shepherd.)
Post: #16
RE: challenge
(01-21-2015 04:03 PM)Hlib Wrote:  I don`t see RPL fonders. Were are you? Whether <Tim Vessman> will be able to answer us something to show advantages of RPL or "banana-products" like HP-NEXT?

I would say that Gilles (below) has shown some advantages of RPL in this type of application. I personally like the straightforward and simple nature of BASIC, but I do appreciate RPL for it's power.
Find all posts by this user
Quote this message in a reply
01-21-2015, 10:07 PM (This post was last modified: 01-21-2015 10:17 PM by Gilles.)
Post: #17
RE: challenge
(01-20-2015 06:10 PM)Hlib Wrote:  I understand your irony :-) For such simple tasks a BASIC is more comfortable. First I wrote this program on CASIO-BASIC for FX-2.0 within an hour, while my algorithm on RPL did not finish yet.
Code:

[[0,3,3,5,4,4,3,5,5,4][3,6,6,8,8,7,7,9,8,8][0,0,6,6,5,5,5,7,6,6]]→Mat K:-1→N:0→K:
For 1→E To 10:Mat K[1,E]+8(E≠1)→S:
  For 1→D To 10:Mat K[1,D]+7(D≠1)+S→T:
    For 1→C To 10:Mat K[3,C]+T→U:(C=2)+1→B:
      For 1→A To 10:Mat K[B,A]+U→V:Isz N:(Frac (N/V)=0)+K→K:
      Next:
    Next:
  Next:
Next:"K=":K

Why a new algorithm ? ;D
Code:

  [[0,3,3,5,4,4,3,5,5,4][3,6,6,8,8,7,7,9,8,8][0,0,6,6,5,5,5,7,6,6]] -1 0 → Km N K
  «
   1 10 FOR E `Km(1,E)+8*(E≠1)` 'S' STO
    1 10 FOR D `Km(1,D)+7*(D≠1)+S` 'T' STO
     1 10 FOR C `Km(3,C)+T` 'U' STO `(C==2)+1` 'B' STO
      1 10 FOR A `Km(B,A)+U`'V' STO 1 'N' STO+ IF V 0 > THEN `(FP(N/V)==0)+K` 'K' STO  END NEXT
     NEXT
    NEXT
   NEXT  K
  »

By the way, it is curious that FRAC (0/0) returns 0 on the Casio
Find all posts by this user
Quote this message in a reply
01-22-2015, 10:10 AM (This post was last modified: 05-23-2016 07:55 PM by Hlib.)
Post: #18
RE: challenge
(01-21-2015 10:07 PM)Gilles Wrote:  Why a new algorithm ? ;D
Code:

  [[0,3,3,5,4,4,3,5,5,4][3,6,6,8,8,7,7,9,8,8][0,0,6,6,5,5,5,7,6,6]] -1 0 → Km N K
  «
   1 10 FOR E `Km(1,E)+8*(E≠1)` 'S' STO
    1 10 FOR D `Km(1,D)+7*(D≠1)+S` 'T' STO
     1 10 FOR C `Km(3,C)+T` 'U' STO `(C==2)+1` 'B' STO
      1 10 FOR A `Km(B,A)+U`'V' STO 1 'N' STO+ IF V 0 > THEN `(FP(N/V)==0)+K` 'K' STO  END NEXT
     NEXT
    NEXT
   NEXT  K
  »

By the way, it is curious that FRAC (0/0) returns 0 on the Casio

Thank you for this report. Command ...Isz N:(Frac (N/V)=0)+K→K:... on CASIO works as ...N+1→N:If N≠0:Then (Frac (N/V)=0)+K→K:IfEnd:... I do appreciate RPL for it's power too, but not in such primitive examples.

EDIT: <primitive> examples
Find all posts by this user
Quote this message in a reply
06-21-2018, 02:43 PM (This post was last modified: 06-21-2018 04:44 PM by Hlib.)
Post: #19
RE: challenge
Completing this old challenge
Code:

<<
[[ 0. 3. 3. 5. 4. 4. 3. 5. 5. 4. ]
[ 3. 6. 6. 8. 8. 7. 7. 9. 8. 8. ]
[ 0. 0. 6. 6. 5. 5. 5. 7. 6. 6. ]]
-1. 0. 0. 0. 0. 0. 0. RCLF ->
m1 n k s t u v b fl
<< 2. SF 3. SF 55. CF 105. SF
1. 10. FOR E 'm1' { 1. E } GET
1. E <> 8. * + 's' STO
1. 10. FOR D 'm1' { 1. D } GET
1. D <> 7. * + s + 't' STO
1. 10. FOR C 'm1' { 3. C } GET
t + 'u' STO C 2. == 1. + 'b' STO
1. 10. FOR A 'm1' { b A } GET
u + 'v' STO 1. 'n' STO+
n 0 <>
<< n v / FP 0. == DUP 'k' STO+
IF 0. <> THEN k n 2. ->LIST END
>> IFT NEXT NEXT NEXT NEXT
k fl STOF
{ 880. 440. 880. 440. 880. 440. }
{ .2 .4 .2 .4 .2 .4 } BEEP
>>
>>
time = 11 min 20 sec
The list of all (359) true elements will be put on stack. You can perform ->LIST to store it in a variable brining stack back into former state.

Link for downloading file in hp-50g or go49g+ .apk emulator:

even_1..9999.hp49 ( 0.93 KB )
Find all posts by this user
Quote this message in a reply
06-22-2018, 12:38 AM
Post: #20
RE: challenge
What about numbers like 1360 thirteen hundred sixty ? Or 1664 sixteen hundred and sixty four ?

- Pauli
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 4 Guest(s)