Post Reply 
another interesting math riddle
01-17-2018, 11:59 PM
Post: #14
RE: another interesting math riddle
(01-13-2018 09:08 PM)olr Wrote:  The following program returns the result in 9 seconds on my Prime rev A (in 19 seconds if the 7-digit optimization is removed).

Code:

EXPORT TST()
BEGIN
    // r result list
    // l list of digits  
    // s factorial sum
    // n helper
  LOCAL 
    r:= {}, 
    l:= {0},
    s, n;

  WHILE SIZE(l) <= 7 DO
  
    s:= ΣLIST(l!);

    IF EQ( l, SORT(ASC(STRING(s))-48) ) THEN
      // The sum of factorials contains exactly
      // the summed up digits
      r(0):= s;
    END;

    // get next list of digits
    // add 1 to the last digit, that is different from 9
    // set succesor digits to the same value
    n:= SIZE(l);
    WHILE n > 0 AND l(n) == 9 DO
      n:= n-1; 
    END;
  
    IF n > 0 THEN
      l(n):= l(n)+1;
      WHILE n < SIZE(l) DO
        // a digit is never smaller than its predecessor
        l(n+1):= l(n); 
        n:= n+1; 
      END;
    ELSE
      // all digits are 9s -> new digit
      l:= MAKELIST(0,X,1,SIZE(l)+1);
    END;  

    // to get a 7-digit number at least three 9s are necessary
    // 8!*5 + 9!*2 = 927360
    IF SIZE(l) == 7 THEN
      l(5):= 9;
      l(6):= 9;
      l(7):= 9;
    END;

  END;
  RETURN r;
END;

I've made a few changes to your program and now it returns the result in less than 7 seconds.

Code:
EXPORT TST()
BEGIN
    // r result list
    // l list of digits  
    // s factorial sum
    // n helper
  LOCAL 
    r:= {}, 
    l:= {0},
    k, s, n;

  WHILE SIZE(l) <= 7 DO
  
    s:= ΣLIST(l!);

    IF EQ( l, SORT(ASC(STRING(s))-48) ) THEN
      // The sum of factorials contains exactly
      // the summed up digits
      r(0):= s;
    END;

    // get next list of digits
    // add 1 to the last digit, that is different from 9
    // set successor digits to the same value
    n:= (POS(l,9)-1) MOD (SIZE(l)+1);
  
    l(n):= (l(n)+1) MOD 10; //if n=0, add a 0 element to l
    FOR k FROM n TO SIZE(l)-1 DO
      // a digit is never smaller than its predecessor
      l(k+1):= l(k); 
    END;

    // to get a 7-digit number at least three 9s are necessary
    // 8!*5 + 9!*2 = 927360
    IF SIZE(l) == 7 THEN 
      l:=CONCAT(l({1,4}),{9,9,9});
    END;

  END;
  RETURN r;
END;
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: another interesting math riddle - olr - 01-13-2018, 09:08 PM
RE: another interesting math riddle - Didier Lachieze - 01-17-2018 11:59 PM
RE: another interesting math riddle - Gamo - 01-14-2018, 10:22 AM



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