35
\$\begingroup\$

Monday numbers, as defined by Gamow in this question over on Puzzling, are positive integers N with the following three properties:

  • The decimal representation of N does not contain the digit 0
  • The decimal representation of N does not contain any digit twice
  • N is divisible by every digit D that occurs in its decimal representation

Note that these are alternatively known, in the OEIS, as Lynch-Bell numbers.

Examples:

  • 15 is a Monday number, as it's divisible by both 1 and 5 and satisfies the other two conditions
  • 16 is not, because it's not divisible by 6.
  • The number 22 is not, because though it satisfies conditions 1 and 3, it fails condition 2.

Here's the list of the first 25 Monday numbers to get you started (there are 548 total):

1 2 3 4 5 6 7 8 9 12 15 24 36 48 124 126 128 132 135 162 168 175 184 216 248

The challenge here is to write the shortest code that generates the full sequence of Monday numbers, from 1 up to 9867312 (proven on that question to be the largest possible).

Your code should take no input, and output should be to STDOUT or equivalent, with your choice of delimiter. All the usual code-golf rules apply, and Standard Loopholes are prohibited.

Leaderboard

var QUESTION_ID=59014,OVERRIDE_USER=42963;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
  • 5
    \$\begingroup\$ Related \$\endgroup\$ – FryAmTheEggman Sep 29 '15 at 15:03
  • 1
    \$\begingroup\$ Also known as Lynch-Bell numbers. \$\endgroup\$ – Geobits Sep 29 '15 at 15:21
  • \$\begingroup\$ @Geobits Thanks - I couldn't find it on OEIS for some reason. \$\endgroup\$ – AdmBorkBork Sep 29 '15 at 15:23
  • 8
    \$\begingroup\$ You should have posted this challenge yesterday... \$\endgroup\$ – mbomb007 Sep 29 '15 at 16:42
  • 2
    \$\begingroup\$ @mbomb007 I would have - didn't see Gamow's question until this morning, though! \$\endgroup\$ – AdmBorkBork Sep 29 '15 at 16:44

46 Answers 46

1
\$\begingroup\$

Jelly, 8 bytes

ȷ7Dg⁼QƲƇ

Runs locally in under eight minutes.

Try it online! (modified to find numbers with six digits or less)

How it works

ȷ7Dg⁼QƲƇ  Main link. No arguments.

ȷ7        Set the return value to 10**7.
       Ƈ  Comb; promote 10**7 to [1, ..., 10**7], then keep only those n in the range
          for which the link to the left returns a truthy value.
      Ʋ     Combine the four links to the left into a monadic chain.
  D           Decimal; yield n's digit array in base 10.
   g          Take the GCD of each digit and n.
     Q        Yield the unique digits of n.
    ⁼         Test both results for equality.
\$\endgroup\$
16
\$\begingroup\$

Python 2, 85 bytes

print[n for n in range(1,9**9)if(n<10**len(set(`n`)))>any(n%(int(d)or.3)for d in`n`)]

Prints a list.

I'm basically combining two of my answers to previous challenges:

Thanks to xsot for 1 byte saved by combining the conditions better.

\$\endgroup\$
  • \$\begingroup\$ You can save a byte: print[n for n in range(1,9**9)if(n<10**len(set(`n`)))>any(n%(int(d)or.3)for d in`n`)] \$\endgroup\$ – xsot Sep 29 '15 at 23:33
11
\$\begingroup\$

Perl, 61 47 bytes

46 bytes code + 1 byte command line parameter.

/(.).*\1|0/||1*s/./$_%$&/rge||print for 1..1e7

Usage:

perl -l entry.pl

Explanation

/(.).*\1|0/ returns 1 if the number-under-test contains a duplicate character or a 0

s/./$_%$&/rge replaces each digit with the value of the number-under-test % the digit. For example, 15 -> 00, 16 -> 04 (because 16%6=4). This means that any input which is divisible by all of its digits will consist of all 0s, otherwise it will contain a digit >0. In order to treat this as a number, we *1, which means any number-under-test will return 0 for this block if it is divisible by all of its digits, otherwise >0.

By separating these two statements and the print with 'or's, if either of the first two conditions returns >0, the condition matches and the subsequent parts of the expression will not evaluate. If and only if both previous conditions are 0, the print will then execute. The -l flag ensures to add a new line after each print.

\$\endgroup\$
  • \$\begingroup\$ Very nice. You can save a few bytes by making it Perl 5.10 and using say instead of print + -l :-) \$\endgroup\$ – xebtl Sep 30 '15 at 13:21
  • \$\begingroup\$ Thanks for the suggestion! I thought say required an explicit declaration first? \$\endgroup\$ – Jarmex Sep 30 '15 at 20:41
  • \$\begingroup\$ @Jarmex I may have started the tradition around here of taking use feature 'say' or use 5.012 for free — I always mention when I do it, and no one has ever challenged it. I've seen a few others do the same :) \$\endgroup\$ – hobbs Oct 1 '15 at 5:45
  • 2
    \$\begingroup\$ @hobbs This answer on meta says “The consensus up to now on SO and here was "the -M5.010, when needed, is free"”. \$\endgroup\$ – xebtl Oct 1 '15 at 8:13
  • 2
    \$\begingroup\$ Using map and say gets this down to 43: Try it online! \$\endgroup\$ – Xcali May 9 '18 at 5:11
10
\$\begingroup\$

Pyth, 22 21

f&.{`T!f%T|vY.3`TS^T7

Thanks to Jakube for golfing off 1 byte of unnecessary formatting.

Heavily inspired by this CW answer to the related question.

I have a paste of the result here, from when it printed newline separated, now it prints as a pythonic list.

I would recommend not trying it online unless you use a number smaller than 7... I've set it to 2 in this link.

Filters from 1 to 10^7-1 which covers all the necessary values. This version may cause a memory error if it cannot make the list S^T7, which is similar to list(range(1,10**7)) in python 3 (However, it works fine for me). If so, you could try:

.f&.{`Z.x!s%LZjZT0548

Which finds the first 548 Monday numbers. This also demonstrates another way to check for the 0s in the number, instead of replacing them with .3 this uses a try-catch block. Credit for this version goes entirely to Jakube. (Note that this is still much to slow for the online interpreter)

\$\endgroup\$
  • 1
    \$\begingroup\$ Here's a different solution: .f&.{`Z.x!s%LZjZT0548 It's quite a bit times faster (4x - 5x) than your while-loop approach and has also only 21 bytes in length. \$\endgroup\$ – Jakube Sep 29 '15 at 18:29
  • 1
    \$\begingroup\$ @Jakube Backticks are a pain in comments aren't they? :P Thanks a lot though! \$\endgroup\$ – FryAmTheEggman Sep 29 '15 at 18:30
  • \$\begingroup\$ Umm.. your solution doesn't seem to work.. In your TIO link in the range to 100, it shows 55, 66, 77, 88, 99, all numbers with duplicated digits.. \$\endgroup\$ – Kevin Cruijssen May 10 '18 at 17:25
  • 1
    \$\begingroup\$ @KevinCruijssen Unfortunately, Pyth has been updated so many times since I made this post, I can't find what has changed. You can see in the paste that this clearly did work before. I think it might be .{ having been changed, since replacing it with {I seems to work. \$\endgroup\$ – FryAmTheEggman May 10 '18 at 20:52
  • \$\begingroup\$ @FryAmTheEggman Ah, I hadn't seen the paste. It's indeed been almost three years, so no wonder things might have changed. +1 in that case, because the paste proves it worked. :) \$\endgroup\$ – Kevin Cruijssen May 10 '18 at 21:04
9
\$\begingroup\$

GS2, 20 19 bytes

gs2 uses a wide range of bytes, not just printable ascii chracters. I will present my solution in hex.

17 7d 2f 24 65 f1 c8 24 d8 62 e9 65 f4 24 40 90 71 f3 54

Here's some explanation. gs2 is a stack based language, so there are no variables. (aside from 4 registers, one of which i use here)

17         # push constant 7
7d         # 10 raised to the power
2f         # create an array of numbers from 1 to n

    24     # get digits of number into array
    65     # calculate product of array
f1         # filter array by previous block of 2 instructions

    c8     # save top of stack to register a
    24     # get digits of number into array
        d8 # tuck register a under top of stack
        62 # boolean divisibility test 
    e9     # map array using previous block of 2 instructions
    65     # calculate product of array
f4         # filter array by previous block of 5 instructions 

    24     # get digits of number into array
    40     # duplicate top of stack
    90     # remove duplicates from array
    71     # test equality
f3         # filter array by previous block of 4 instructions
54         # show contents of array separated by line breaks
\$\endgroup\$
8
\$\begingroup\$

Python 3, 132 128 114 111 104 bytes

i=0
while i<1e8:
 j=str(i)
 if len(set(j))+2==len(j)+('0'in j)+all(i%int(k)<1 for k in j):print(i)
 i+=1

There are 548 Monday Numbers.

\$\endgroup\$
  • 1
    \$\begingroup\$ Could you use 1e8 instead of even 9**9? \$\endgroup\$ – Dom Hastings Sep 29 '15 at 15:23
  • \$\begingroup\$ Remove the space in '0' not. Also, i%int(k)==0 can probably be i%int(k)<1? \$\endgroup\$ – mbomb007 Sep 29 '15 at 16:43
  • \$\begingroup\$ Thanks. I didn't mean to add that back in. @mbomb007 \$\endgroup\$ – Zach Gates Sep 29 '15 at 16:43
  • \$\begingroup\$ You can use j=`i`. \$\endgroup\$ – mbomb007 Sep 29 '15 at 16:47
  • \$\begingroup\$ For another -6 use if len(set(j))+2==len(j)+('0'in j)+all(i%int(k)<1 for k in j) \$\endgroup\$ – lirtosiast Sep 29 '15 at 20:03
7
\$\begingroup\$

APL, 44 39 37 bytes

{0=+/(⊢|∘⍵,0∘∊,⍴∘⊢≠⍴∘∪)⍎¨⍕⍵:⍵⋄⍬}¨⍳1e7

Ungolfed:

{
 x ← ⍎¨⍕⍵⋄                    ⍝ Define x to be a vector of the digits of ⍵
 0=+/(⊢|∘⍵,0∘∊,⍴∘⊢≠⍴∘∪)x:   ⍝ No zeros, all digits divide ⍵, all unique?
 ⍵⋄⍬                          ⍝ If so, return the input, otherwise null
}¨⍳1e7                        ⍝ Apply to the integers 1..1E7

Saved 7 bytes thanks to Moris Zucca!

\$\endgroup\$
  • \$\begingroup\$ I love APL. This is why. \$\endgroup\$ – Conor O'Brien Sep 30 '15 at 1:07
  • \$\begingroup\$ I think you can golf it using function trains, saving 5 bytes: {0=+/(⊢|∘⍵,0∘∊,⍴∘⊢≠⍴∘∪)x←⍎¨⍕⍵:⍵⋄⍬}¨⍳1e7 \$\endgroup\$ – Moris Zucca Sep 30 '15 at 13:59
  • \$\begingroup\$ @MorisZucca Awesome, thanks for the suggestion! \$\endgroup\$ – Alex A. Oct 1 '15 at 19:41
  • \$\begingroup\$ I just saw that in this form x← is not needed any more, so 2 more bytes saved! :-) \$\endgroup\$ – Moris Zucca Oct 2 '15 at 7:57
  • \$\begingroup\$ @MorisZucca You're an APL golfing machine! Thanks again! \$\endgroup\$ – Alex A. Oct 2 '15 at 18:26
6
\$\begingroup\$

TI-BASIC, 60 59 bytes

For(X,1,ᴇ7
int(10fPart(X10^(-randIntNoRep(1,1+int(log(X->D
SortA(∟D
If X>9
If not(max(remainder(X,Ans+2Xnot(Ansmin(ΔList(∟D
Disp X
End

∟D is the list of digits, which is generated using math and the randIntNoRep( command (random permutation of all integers between 1 and 1+int(log(X inclusive). I use a slightly complicated chain of statements to check if all of the conditions are satisfied:

   min(ΔList(∟D        ;Zero if repeated digit, since ∟D was sorted ascending
Ans                    ;Multiplies the unsorted copy of ∟D by the minimum from above
                       ;(Lists are different dimensions; we can't elementwise AND)
                       ;Will contain a 0 if there's a 0 digit or a repeated digit
      not(             ;If there's a zero,
Ans+2X                 ;Add 2X to that pos. in the list, failing the test:

    max(remainder(X,   ;Zero iff all digits divide X and 2X wasn't added
not(

To fail numbers that have repeated digits or zero digits, I replace zeroes with 2X, because X is never divisible by 2X.

To special-case 1~9 (because ΔList( on a one-element list errors) I use the If statement in the fourth line to skip over the check in the fifth line, automatically displaying all X≤9.

The output numbers are separated by newlines.

\$\endgroup\$
5
\$\begingroup\$

Mathematica 105

l=Length;Cases[Range@9867312,n_ /;(FreeQ[i=IntegerDigits@n,0]&&l@i== l@Union@i&&And@@(Divisible[n,#]&/@i))]
  • IntegerDigits breaks up n into a list of its digits, i.
  • FreeQ[i,0] checks whether there are no zeros in the list.
  • Length[i]==Length[Union[i]] checks that there are no repeated digits.
  • And@@(Divisible[n,#]&/@i) checks that each digit is a divisor of n.

{1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 24, 36, 48, 124, 126, 128, 132, 135, 162, 168, 175, 184, 216, 248, 264, 312, 315, 324, 384, 396, 412, 432, 612, 624, 648, 672, 728, 735, 784, 816, 824, 864, 936, 1236, 1248, 1296, 1326, 1362, 1368, 1395, 1632, 1692, 1764, 1824, 1926, 1935, 1962, 2136, 2184, 2196, 2316, 2364, 2436, 2916, 3126, 3162, 3168, 3195, 3216, 3264, 3276, 3492, 3612, 3624, 3648, 3816, 3864, 3915, 3924, 4128, 4172, 4236, 4368, 4392, 4632, 4872, 4896, 4932, 4968, 6132, 6192, 6312, 6324, 6384, 6432, 6912, 6984, 8136, 8496, 8736, 9126, 9135, 9162, 9216, 9315, 9324, 9432, 9612, 9648, 9864, 12384, 12648, 12768, 12864, 13248, 13824, 13896, 13968, 14328, 14728, 14832, 16248, 16824, 17248, 18264, 18432, 18624, 18936, 19368, 21384, 21648, 21784, 21864, 23184, 24168, 24816, 26184, 27384, 28416, 29736, 31248, 31824, 31896, 31968, 32184, 34128, 36792, 37128, 37296, 37926, 38472, 39168, 39816, 41328, 41832, 42168, 42816, 43128, 43176, 46128, 46872, 48216, 48312, 61248, 61824, 62184, 64128, 68712, 72184, 73164, 73248, 73416, 73962, 78624, 79128, 79632, 81264, 81432, 81624, 81936, 82416, 84216, 84312, 84672, 87192, 89136, 89712, 91368, 91476, 91728, 92736, 93168, 93816, 98136, 123648, 123864, 123984, 124368, 126384, 129384, 132648, 132864, 132984, 134928, 136248, 136824, 138264, 138624, 139248, 139824, 142368, 143928, 146328, 146832, 148392, 148632, 149328, 149832, 162384, 163248, 163824, 164328, 164832, 167328, 167832, 168432, 172368, 183264, 183624, 184392, 184632, 186432, 189432, 192384, 193248, 193824, 194328, 194832, 198432, 213648, 213864, 213984, 214368, 216384, 218736, 219384, 231648, 231864, 231984, 234168, 234816, 236184, 238416, 239184, 241368, 243168, 243768, 243816, 247968, 248136, 248976, 261384, 263184, 273168, 281736, 283416, 284136, 291384, 293184, 297864, 312648, 312864, 312984, 314928, 316248, 316824, 318264, 318624, 319248, 319824, 321648, 321864, 321984, 324168, 324816, 326184, 328416, 329184, 341928, 342168, 342816, 346128, 348192, 348216, 348912, 349128, 361248, 361824, 361872, 362184, 364128, 364728, 367248, 376824, 381264, 381624, 382416, 384192, 384216, 384912, 391248, 391824, 392184, 394128, 412368, 413928, 416328, 416832, 418392, 418632, 419328, 419832, 421368, 423168, 423816, 427896, 428136, 428736, 431928, 432168, 432768, 432816, 436128, 438192, 438216, 438912, 439128, 461328, 461832, 463128, 468312, 469728, 478296, 478632, 481392, 481632, 482136, 483192, 483216, 483672, 483912, 486312, 489312, 491328, 491832, 493128, 498312, 612384, 613248, 613824, 613872, 614328, 614832, 618432, 621384, 623184, 623784, 627984, 631248, 631824, 632184, 634128, 634872, 641328, 641832, 643128, 648312, 671328, 671832, 681432, 684312, 689472, 732648, 732816, 742896, 746928, 762384, 768432, 783216, 789264, 796824, 813264, 813624, 814392, 814632, 816432, 819432, 823416, 824136, 824376, 831264, 831624, 832416, 834192, 834216, 834912, 836472, 841392, 841632, 842136, 843192, 843216, 843912, 846312, 849312, 861432, 864312, 873264, 891432, 894312, 897624, 912384, 913248, 913824, 914328, 914832, 918432, 921384, 923184, 927864, 931248, 931824, 932184, 934128, 941328, 941832, 943128, 948312, 976248, 978264, 981432, 984312, 1289736, 1293768, 1369872, 1372896, 1376928, 1382976, 1679328, 1679832, 1687392, 1738296, 1823976, 1863792, 1876392, 1923768, 1936872, 1982736, 2137968, 2138976, 2189376, 2317896, 2789136, 2793168, 2819376, 2831976, 2931768, 2937816, 2978136, 2983176, 3186792, 3187296, 3196872, 3271968, 3297168, 3298176, 3619728, 3678192, 3712968, 3768912, 3796128, 3816792, 3817296, 3867192, 3869712, 3927168, 3928176, 6139728, 6379128, 6387192, 6389712, 6391728, 6719328, 6719832, 6731928, 6893712, 6913872, 6971328, 6971832, 7168392, 7198632, 7231896, 7291368, 7329168, 7361928, 7392168, 7398216, 7613928, 7639128, 7829136, 7836192, 7839216, 7861392, 7863912, 7891632, 7892136, 7916328, 7916832, 7921368, 8123976, 8163792, 8176392, 8219736, 8312976, 8367912, 8617392, 8731296, 8796312, 8912736, 8973216, 9163728, 9176328, 9176832, 9182376, 9231768, 9237816, 9278136, 9283176, 9617328, 9617832, 9678312, 9718632, 9723168, 9781632, 9782136, 9812376, 9867312}

Length[%]

548

\$\endgroup\$
  • \$\begingroup\$ I expect there is a way in mathematica to get large numbers in fewer bytes, like 9^9 or 1e8 or something \$\endgroup\$ – FryAmTheEggman Sep 29 '15 at 15:30
  • \$\begingroup\$ I'm surprised Mathematica doesn't have a built-in for this ;-). Nice trick with the Union to check for duplicates. \$\endgroup\$ – AdmBorkBork Sep 29 '15 at 15:48
  • \$\begingroup\$ @FryAmTheEggman, You are correct about Mathematica allowing for 9^9. But wouldn't that return more than 548 Monday numbers? \$\endgroup\$ – DavidC Sep 29 '15 at 16:27
  • \$\begingroup\$ As said in the question, there is no possible Monday number greater than the one given as the upper limit. \$\endgroup\$ – FryAmTheEggman Sep 29 '15 at 16:28
5
\$\begingroup\$

Haskell, 77 bytes

[x|x<-[1..9^9],all(\a->a>'0'&&mod x(read[a])+sum[1|y<-show x,y==a]<2)$show x]

Usage example (the first 20 numbers):

take 20 $ [x|x<-[1..9^9],all(\a->a>'0'&&mod x(read[a])+sum[1|y<-show x,y==a]<2)$show x]

[1,2,3,4,5,6,7,8,9,12,15,24,36,48,124,126,128,132,135,162]

How it works: iterate over all numbers from 1 to 9^9 and check the conditions. The current number x is turned into it's string representation (show x) to operate on it as a list of characters.

\$\endgroup\$
5
\$\begingroup\$

R, 99 bytes

for(n in 1:1e8){i=1:nchar(n);if(all(table(d<-(n%%10^i)%/%10^(i-1))<2)&!0%in%d&all(!n%%d))cat(n,"")}

Slightly less golfed:

for(n in 1:1e8){
    i = 1:nchar(n)
    d = (n%%10^i)%/%10^(i-1) # Digits of n
    if(all(table(d)<2) # No digits is present more than once 
      & !0%in%d        # 0 is not one of the digits
      & all(!n%%d))    # All digits are divisors of n
    cat(n,"")
    }
\$\endgroup\$
5
\$\begingroup\$

Perl, 90 75 70 bytes

print+($_,$/)x(grep(!/(\d).*\1|0/,$_)&s/./!$&||$_%$&/ger<1)for 1..1e7
\$\endgroup\$
  • 1
    \$\begingroup\$ Ahhh, I missed the \1 trick for dupe checking, nice. Can you save more with a statement modifier while and a ternary print? \$\endgroup\$ – Dom Hastings Sep 30 '15 at 20:17
  • \$\begingroup\$ @DomHastings thanks, now more golfed using your suggestion \$\endgroup\$ – steve Sep 30 '15 at 21:11
  • \$\begingroup\$ Nice, I think you can save a few more as well, as you don't need the ^ and the $ around the 0 in your grep, you can replace the && before s/./ with a single & and I think the last |0 is unneeded (although only tested up-to 1e3...). Well and truly thrashed my score! :) \$\endgroup\$ – Dom Hastings Oct 1 '15 at 4:48
  • 1
    \$\begingroup\$ @DomHastings thanks, down to 70 with your golfing tips. \$\endgroup\$ – steve Oct 1 '15 at 7:03
  • \$\begingroup\$ Golfed it down a bit more by getting rid of the grep (unnecessary--the pattern match takes care of it without grep) and rearranging the rest into a map: Try it online! \$\endgroup\$ – Xcali Jan 3 at 16:57
4
\$\begingroup\$

CJam, 25 bytes

1e7{_Ab__&0-_@=@@f%1b>},`

Try it online. Note that online link only runs to 10,000. I'm not sure if it would finish online if you're patient enough. It haven't tested it with the offline version of CJam, but I expect that it would terminate.

Explanation:

1e7     Upper limit.
{       Start filter loop.
  _Ab     Copy and convert to list of decimal digits.
  __&     Intersect list with itself to remove duplicates.
  0-      Remove zero.
  _       Make a copy of unique non-zero digits. Will use these as divisors.
  @=      Compare unique non-zero digits to all digits. Must be true for Monday numbers.
  @@      Rotate original number and list of non-zero digits to top.
  f%      Remainders of original number with all non-zero digits.
  1b      Sum up the remainders. Since they all must be zero for Monday numbers,
          their sum must be zero.
  >       Check that first part of condition was 1, and sum of remainders 0.
},      End filter loop.
`       Convert resulting list to string.
\$\endgroup\$
4
\$\begingroup\$

C#, 230 227

It's been a while since I've golved so I probably forgot a few tricks to get the bytecount down. Will improve when I think of them... For now:

using System.Linq;class P{static void Main(){System.Console.Write(string.Join(",",Enumerable.Range(0,1<<24).Where(i=>{var s=i.ToString();return!s.Contains('0')&&s.Length==s.Distinct().Count()&&s.All(x=>i%(48-(int)x)==0);})));}}

Ungolfed:

using System.Linq;
class P
{
    static void Main()
    {
        System.Console.Write(                                       //Output...
            string.Join(                                            //...all results...
                ",",                                                //...comma separated...
                Enumerable.Range(0, 1<<24)                          //...from 0 to 16777216...
                    .Where(i => {                                   //...where...
                        var s = i.ToString();                       //...the digits as char array (what we usually call a string)...
                        return !s.Contains('0')                     //...for which none of the digits is 0...
                            && s.Length == s.Distinct().Count()     //...and the number of distinct digits equals the total number of digits (e.g. all unique)...
                            && s.All(x => i % (48 - (int)x) == 0);  //...and the number is divisible by each of the digits (after 'ASCII-correction')
                    })
            )
        );
    }
}

1,2,3,4,5,6,7,8,9,12,15,24,36,48,124,126,128,132,135,162,168,175,184,216,248,264,312,315,324,384,396,412,432,612,624,648,672,728,735,784,816,824,864,936,1236,1248,1296,1326,1362,1368,1395,1632,1692,1764,1824,1926,1935,1962,2136,2184,2196,2316,2364,2436,2916,3126,3162,3168,3195,3216,3264,3276,3492,3612,3624,3648,3816,3864,3915,3924,4128,4172,4236,4368,4392,4632,4872,4896,4932,4968,6132,6192,6312,6324,6384,6432,6912,6984,8136,8496,8736,9126,9135,9162,9216,9315,9324,9432,9612,9648,9864,12384,12648,12768,12864,13248,13824,13896,13968,14328,14728,14832,16248,16824,17248,18264,18432,18624,18936,19368,21384,21648,21784,21864,23184,24168,24816,26184,27384,28416,29736,31248,31824,31896,31968,32184,34128,36792,37128,37296,37926,38472,39168,39816,41328,41832,42168,42816,43128,43176,46128,46872,48216,48312,61248,61824,62184,64128,68712,72184,73164,73248,73416,73962,78624,79128,79632,81264,81432,81624,81936,82416,84216,84312,84672,87192,89136,89712,91368,91476,91728,92736,93168,93816,98136,123648,123864,123984,124368,126384,129384,132648,132864,132984,134928,136248,136824,138264,138624,139248,139824,142368,143928,146328,146832,148392,148632,149328,149832,162384,163248,163824,164328,164832,167328,167832,168432,172368,183264,183624,184392,184632,186432,189432,192384,193248,193824,194328,194832,198432,213648,213864,213984,214368,216384,218736,219384,231648,231864,231984,234168,234816,236184,238416,239184,241368,243168,243768,243816,247968,248136,248976,261384,263184,273168,281736,283416,284136,291384,293184,297864,312648,312864,312984,314928,316248,316824,318264,318624,319248,319824,321648,321864,321984,324168,324816,326184,328416,329184,341928,342168,342816,346128,348192,348216,348912,349128,361248,361824,361872,362184,364128,364728,367248,376824,381264,381624,382416,384192,384216,384912,391248,391824,392184,394128,412368,413928,416328,416832,418392,418632,419328,419832,421368,423168,423816,427896,428136,428736,431928,432168,432768,432816,436128,438192,438216,438912,439128,461328,461832,463128,468312,469728,478296,478632,481392,481632,482136,483192,483216,483672,483912,486312,489312,491328,491832,493128,498312,612384,613248,613824,613872,614328,614832,618432,621384,623184,623784,627984,631248,631824,632184,634128,634872,641328,641832,643128,648312,671328,671832,681432,684312,689472,732648,732816,742896,746928,762384,768432,783216,789264,796824,813264,813624,814392,814632,816432,819432,823416,824136,824376,831264,831624,832416,834192,834216,834912,836472,841392,841632,842136,843192,843216,843912,846312,849312,861432,864312,873264,891432,894312,897624,912384,913248,913824,914328,914832,918432,921384,923184,927864,931248,931824,932184,934128,941328,941832,943128,948312,976248,978264,981432,984312,1289736,1293768,1369872,1372896,1376928,1382976,1679328,1679832,1687392,1738296,1823976,1863792,1876392,1923768,1936872,1982736,2137968,2138976,2189376,2317896,2789136,2793168,2819376,2831976,2931768,2937816,2978136,2983176,3186792,3187296,3196872,3271968,3297168,3298176,3619728,3678192,3712968,3768912,3796128,3816792,3817296,3867192,3869712,3927168,3928176,6139728,6379128,6387192,6389712,6391728,6719328,6719832,6731928,6893712,6913872,6971328,6971832,7168392,7198632,7231896,7291368,7329168,7361928,7392168,7398216,7613928,7639128,7829136,7836192,7839216,7861392,7863912,7891632,7892136,7916328,7916832,7921368,8123976,8163792,8176392,8219736,8312976,8367912,8617392,8731296,8796312,8912736,8973216,9163728,9176328,9176832,9182376,9231768,9237816,9278136,9283176,9617328,9617832,9678312,9718632,9723168,9781632,9782136,9812376,9867312

\$\endgroup\$
  • \$\begingroup\$ can (int)1e7 be 1<<24? \$\endgroup\$ – lirtosiast Oct 2 '15 at 8:52
  • \$\begingroup\$ @ThomasKwa Yes, it can be. Indeed. Thanks! \$\endgroup\$ – RobIII Oct 2 '15 at 12:08
4
+200
\$\begingroup\$

TI-BASIC, 55 53 bytes

This is a relatively minor edit of Thomas Kwa's answer, but I am submitting it as a new answer because I'd heard that he has put a bounty on golfing his TI-BASIC answers.

For(X,1,ᴇ7
int(10fPart(X10^(-randIntNoRep(0,1+int(log(X->D
SortA(∟D
If not(sum(remainder(X,Ans+Xnot(Ansmin(ΔList(∟D
Disp X
End

My main change is from randIntNoRep(1, to randIntNoRep(0, meaning that there will now a zero in every generated list of digits.

number  |  randIntNoRep  |  digits  |  sorted
9       |  1,0           |  9,0     |  0,9
102     |  3,1,0,2       |  1,2,0,0 |  0,0,1,2

Since there's now a zero in every set of digits, this affects the sum of the remainders. Normally the sum of the remainders is 0, but now, the presence of an extra zero causes one failure of our divisibility test.
To counteract this, I changed 2Xnot( to Xnot(. The 2 was originally there to make the test fail at 0, but now it passes at zero. Numbers that contain a zero in their digits, however, now have a min(ΔList(∟D of zero anyways (since there's 2 or more zeros in their lists) so this change does not cause any extra numbers to pass the test.

The benefit of this method is that, since there are now "two digits" produced from the number 1-9, the ΔList( function does not produce an error, allowing us to get rid of a special condition for single-digit numbers.

\$\endgroup\$
4
\$\begingroup\$

05AB1E, 30 22 21 18 14 13 12 9 bytes

-9 byte thanks to the help and encouragement of @Enigma and @Mr.Xcoder. Thanks for letting me mostly figure it out myself, even though you already had a 12-byte solution in mind when I was still at 30. Learned a lot about 05AB1E from this challenge!
-3 bytes thanks to @Grimy

7°LʒÐÑÃÙQ

Try it online (only outputs the numbers below 103 instead of 107 to prevent a timeout after 60 sec).

Explanation:

7°L        # Generate a list in the range [1, 10^7]
   ʒ       # Filter, so only the numbers that evaluated to 1 (truthy) remain:
    Ð      #  Triplicate the current number
     Ñ     #  Get the divisors of this number
           #   i.e. 128 → [1,2,4,8,16,32,64,128]
           #   i.e. 1210 → [1,2,5,10,11,22,55,110,121,242,605,1210]
      Ã    #  Only keep those digits/numbers in the original number (which is checked in
           #  order, so it will only keep the digits and ignores the later numbers)
           #   i.e. 128 → 128
           #   i.e. 1210 → 121
       Ù   #  Uniquify the number, removing any duplicated digits
           #   i.e. 128 → 128
           #   i.e. 121 → 12
        Q  #  Check if the number is unchanged after this
           #   i.e. 128 and 128 → 1 (truthy)
           #   i.e. 1210 and 12 → 0 (falsey)

Previous 12 byter version (one of my very first 05AB1E answers):
NOTE: Only works in the legacy version of 05AB1E.

7°LʒÐSÖPsDÙQ*

Try it online (only outputs the numbers below 103 instead of 107 to prevent a timeout after 60 sec).

Explanation:

7°L        # Generate a list in the range [1, 10^7]
   ʒ       # Filter, so only the numbers that evaluated to 1 (true) remain:
    Ð      #  Triplicate the current number N
     Ù     #  Remove all duplicated digits of the second N
           #   i.e. 1210 → 120
      Q    #  Check if the last two numbers are still the same (1 or 0 as result)
    *      #  Multiply this result with remaining third number from the triplication
     D     #  Duplicate this number, so we have two again
      S    #  Separate all the digits of the second one
           #   i.e. 128 → ['1', '2', '8']
       Ö   #  Check if (the second) N is divisible by each of its digits
           #   i.e. 128 and ['1', '2', '8'] → [1, 1, 1]
           #   (NOTE: If the number contains a '0', it won't error on division by 0,
           #          but instead return the number N itself in the list)
           #   i.e. 105 and ['1', '0', '5'] → [1, 105, 1]
        P  #  Take the product of this list (if the divisible test for one
           #  of the digits was 0, this will be 0 as well)
           #   i.e. [1, 1, 1] → 1
           #   i.e. [1, 105, 1] → 105 (only 1 is truthy in 05AB1E)
\$\endgroup\$
  • \$\begingroup\$ Your answer prints 297, which is not in the sequence of Lynch-Bell numbers. \$\endgroup\$ – Mr. Xcoder May 9 '18 at 13:25
  • \$\begingroup\$ @Mr.Xcoder Sigh.. Had something longer at first for checking if a number is divisible by all its digits, but figured a challenge like that existed. It seems this answer is therefore also invalid.. And here you and Enigma are talking about 12-15 byte answers while my 30-byte answer doesn't even work, lol.. Is there a tutorial anywhere? ;p \$\endgroup\$ – Kevin Cruijssen May 9 '18 at 13:33
  • 1
    \$\begingroup\$ 9 bytes: 7°LʒÐÑÃÙQ \$\endgroup\$ – Grimmy May 7 at 13:56
  • \$\begingroup\$ @Grimy One of my very first 05AB1E answers. :) Nice approach! \$\endgroup\$ – Kevin Cruijssen May 7 at 14:16
3
\$\begingroup\$

Julia, 88 bytes

print(join(filter(i->(d=digits(i);0∉d&&d==unique(d)&&all(j->i%j<1,d)),1:9867312)," "))

This simply takes all numbers from 1 up to the largest Lynch-Bell number and filters them down to only the Lynch-Bell numbers.

Ungolfed:

lynch = filter(i -> (d = digits(i);
                     0 ∉ d &&
                     d == unique(d) &&
                     all(j -> i % j == 0, d)),
               1:9867312)

print(join(lynch, " "))
\$\endgroup\$
3
\$\begingroup\$

Python 2, 101 bytes

print[i for i in range(6**9)if'0'not in`i`and len(set(`i`))==len(`i`)and all(i%int(k)==0for k in`i`)]

You can omit the print in the interpreter get to 96. Used 6**9 since it is 8 digits while the largest monday number is only 7 digits, something like 9**9 would probably take a long time, 6**9 only takes about 10 seconds.

\$\endgroup\$
  • \$\begingroup\$ As pointed out on a couple of questions 1e7 is shorter than both \$\endgroup\$ – Holloway Oct 2 '15 at 8:58
  • \$\begingroup\$ @Trengot 1e7 is a float, range takes integers. \$\endgroup\$ – Rohcana Oct 2 '15 at 9:12
  • \$\begingroup\$ Very true. Hadn't thought of that \$\endgroup\$ – Holloway Oct 2 '15 at 9:14
3
\$\begingroup\$

Perl, 97 bytes

print+($n=$_,$/)x(!/0/&(y///c==grep{2>eval"$n=~y/$_//"}/./g)&&y///c==grep!($n%$_),/./g)for 1..1e7

Takes a while to run, but produces the required output, change to 1e3 for a quicker example!

\$\endgroup\$
  • \$\begingroup\$ I'm not in a position to try this at the moment but, instead of y///c==grep{2>eval"$n=~y/$_//"}/./g, could you use something along the lines of !/(.).*\1/? \$\endgroup\$ – msh210 May 11 '18 at 9:04
  • \$\begingroup\$ @msh210 Almost certainly! I think that would be my default now, but changing this would only end up making it closer to steve's or Jarmex's answers, which are far superior! Thanks for taking a look though! \$\endgroup\$ – Dom Hastings May 11 '18 at 9:23
3
\$\begingroup\$

MATLAB, 100

o=49;for n=2:1e7 a=num2str(n);if all([diff(sort(a)) a~=48 ~mod(n,a-48)]) o=[o ',' a];end;end;disp(o)

And in a more readable format:

o=49;  %1 is always in there, so add the ASCII value. This prevents there being a ',' prefixed.
for n=2:1e7 
    a=num2str(n);
    if (all([diff(sort(a)) a~=48 ~mod(n,a-48)]))
        o=[o ',' a];
    end
end
disp(o)

Basically this counts through every number between \$1\$ and \$1\times10^7\$ and checks if they are a Monday number. Each number is converted to a string so that the digits can be dealt with individually.

The checks are as follows:

  1. First check if there are any duplicates. By sorting the array, if the difference between any consecutive digits is zero, then there are duplicates

    diff(sort(a))
    
  2. Check if there are any zeros. The ASCII for 0 is 48, so we check that all digits are not equal to that.

    a~=48
    
  3. Check if it is divisible by all its digits. We check that the remainder when dividing by each digit (converted from ASCII to decimal, hence -48) is zero.

    ~mod(n,a-48)
    

Finally we make sure that all() the checks are true, and if so we append it to a comma separated output string.

MATLAB has no STDOUT, so instead I print the result string at the end using disp()


This code is SLOW! I am still running it to make sure that it correctly finds all the Monday numbers, but looks good so far.

Update:

Code finished running. It prints the following:

1,2,3,4,5,6,7,8,9,12,15,24,36,48,124,126,128,132,135,162,168,175,184,216,248,264,312,315,324,384,396,412,432,612,624,648,672,728,735,784,816,824,864,936,1236,1248,1296,1326,1362,1368,1395,1632,1692,1764,1824,1926,1935,1962,2136,2184,2196,2316,2364,2436,2916,3126,3162,3168,3195,3216,3264,3276,3492,3612,3624,3648,3816,3864,3915,3924,4128,4172,4236,4368,4392,4632,4872,4896,4932,4968,6132,6192,6312,6324,6384,6432,6912,6984,8136,8496,8736,9126,9135,9162,9216,9315,9324,9432,9612,9648,9864,12384,12648,12768,12864,13248,13824,13896,13968,14328,14728,14832,16248,16824,17248,18264,18432,18624,18936,19368,21384,21648,21784,21864,23184,24168,24816,26184,27384,28416,29736,31248,31824,31896,31968,32184,34128,36792,37128,37296,37926,38472,39168,39816,41328,41832,42168,42816,43128,43176,46128,46872,48216,48312,61248,61824,62184,64128,68712,72184,73164,73248,73416,73962,78624,79128,79632,81264,81432,81624,81936,82416,84216,84312,84672,87192,89136,89712,91368,91476,91728,92736,93168,93816,98136,123648,123864,123984,124368,126384,129384,132648,132864,132984,134928,136248,136824,138264,138624,139248,139824,142368,143928,146328,146832,148392,148632,149328,149832,162384,163248,163824,164328,164832,167328,167832,168432,172368,183264,183624,184392,184632,186432,189432,192384,193248,193824,194328,194832,198432,213648,213864,213984,214368,216384,218736,219384,231648,231864,231984,234168,234816,236184,238416,239184,241368,243168,243768,243816,247968,248136,248976,261384,263184,273168,281736,283416,284136,291384,293184,297864,312648,312864,312984,314928,316248,316824,318264,318624,319248,319824,321648,321864,321984,324168,324816,326184,328416,329184,341928,342168,342816,346128,348192,348216,348912,349128,361248,361824,361872,362184,364128,364728,367248,376824,381264,381624,382416,384192,384216,384912,391248,391824,392184,394128,412368,413928,416328,416832,418392,418632,419328,419832,421368,423168,423816,427896,428136,428736,431928,432168,432768,432816,436128,438192,438216,438912,439128,461328,461832,463128,468312,469728,478296,478632,481392,481632,482136,483192,483216,483672,483912,486312,489312,491328,491832,493128,498312,612384,613248,613824,613872,614328,614832,618432,621384,623184,623784,627984,631248,631824,632184,634128,634872,641328,641832,643128,648312,671328,671832,681432,684312,689472,732648,732816,742896,746928,762384,768432,783216,789264,796824,813264,813624,814392,814632,816432,819432,823416,824136,824376,831264,831624,832416,834192,834216,834912,836472,841392,841632,842136,843192,843216,843912,846312,849312,861432,864312,873264,891432,894312,897624,912384,913248,913824,914328,914832,918432,921384,923184,927864,931248,931824,932184,934128,941328,941832,943128,948312,976248,978264,981432,984312,1289736,1293768,1369872,1372896,1376928,1382976,1679328,1679832,1687392,1738296,1823976,1863792,1876392,1923768,1936872,1982736,2137968,2138976,2189376,2317896,2789136,2793168,2819376,2831976,2931768,2937816,2978136,2983176,3186792,3187296,3196872,3271968,3297168,3298176,3619728,3678192,3712968,3768912,3796128,3816792,3817296,3867192,3869712,3927168,3928176,6139728,6379128,6387192,6389712,6391728,6719328,6719832,6731928,6893712,6913872,6971328,6971832,7168392,7198632,7231896,7291368,7329168,7361928,7392168,7398216,7613928,7639128,7829136,7836192,7839216,7861392,7863912,7891632,7892136,7916328,7916832,7921368,8123976,8163792,8176392,8219736,8312976,8367912,8617392,8731296,8796312,8912736,8973216,9163728,9176328,9176832,9182376,9231768,9237816,9278136,9283176,9617328,9617832,9678312,9718632,9723168,9781632,9782136,9812376,9867312

Which if you run this code with that as the input:

nums = length(strsplit(stdout,','))

Yeilds 548.

\$\endgroup\$
3
\$\begingroup\$

Ruby, 79

?1.upto(?9*7){|s|a=s.chars;a.uniq!||a.any?{|x|x<?1||0<eval([s,x]*?%)}||puts(s)}

More interesting but slightly longer solution with a regex:

?1.upto(?9*7){|s|s[/(.).*\1|[0#{(1..9).map{|*x|x*eval([s,x]*?%)}*''}]/]||puts(s)}

In each case, we're using Ruby's ability to iterate over strings as though they were decimal integers: ?1.upto(?9*7) is equivalent to 1.upto(9999999).map(&:to_s).each. We join the string to each nonzero digit using the modulo operator, and eval the result, to check for divisibility.

Bonus Ruby 1.8 solution (requires -l flag for proper output):

'1'.upto('9'*7){|$_|~/(.).*\1|[0#{(1..9).map{|*x|x*eval("#$_%#{x}")}}]/||print}

1.8 allowed the block iterator to be a global variable. Assigning to $_ makes it the implicit receiver for string operations. We also get to interpolate arrays into the regular expression more easily: in 1.8, /[#{[1,2]}]/ evaluates to /[12]/.

\$\endgroup\$
3
\$\begingroup\$

Pip, 25 bytes

Fa,t**7Ia#=UQa&0=$+a%^aPa

Outputs each number on its own line. This has been running for about 10 minutes and gotten up to 984312 so far, but I'm pretty sure it's correct. (Edit: Couple hours later... code finished, generated all 548 of 'em.)

Here's a Python-esque pseudocode rendition:

for a in range(10**7):
  if lengthEqual(a, set(a)) and 0 == sum(a%d for d in digits(a)):
    print(a)

The #= operator compares two iterables by length. If the number of UniQue characters in a is the same as the number of characters in a, there are no repeats.

The divisible-by-each-digit check is from one of my Pip example programs. I wrote it after seeing the earlier challenge, but didn't post it there because the language was newer than the question. Otherwise, at 8 bytes, it would be the winning answer to that question. Here's a step-by-step explanation:

      ^a   Split num into an array of its digits
    a%     Take num mod each of those digits; if a digit is zero, the result will be nil
  $+       Sum the resulting list (note: summing a list containing nil results in nil!)
0=         Iff the sum equals 0, return 1 (true); otherwise (>0 or nil), return 0 (false)
\$\endgroup\$
  • \$\begingroup\$ This is a pretty neat language! Nice to see something other than stack-based golfing. \$\endgroup\$ – AdmBorkBork Sep 30 '15 at 3:07
  • 1
    \$\begingroup\$ @TimmyD If you want to see non-stack based golfing, there tends to be quite a bit of Pyth around. \$\endgroup\$ – Reto Koradi Oct 1 '15 at 5:06
  • \$\begingroup\$ @RetoKoradi But if you want to see non-stack-based golfing with infix operators, Pip is for you. ;^) \$\endgroup\$ – DLosc Oct 1 '15 at 19:06
  • \$\begingroup\$ Couple hours later It's a good thing performance isn't taken into account. \$\endgroup\$ – Holloway Oct 2 '15 at 9:01
3
\$\begingroup\$

Javascript (ES6), 106 90 83 bytes

Kids, don't try this at home; JS will not be happy with the prospect of looping through every digit of every integer from one to ten million with a regex.

for(i=0;i<1e7;i++)/(.).*\1|0/.test(i)||+`${i}`.replace(/./g,j=>i%j)||console.log(i)

The first regex (props to @Jarmex) returns true if the number contains duplicate digits or zeroes. If this turns out false, the program move on to the second, which replaces each digit j with i%j. The result is all zeroes if it's divisible by all of it's digits, in which case it moves on to console.log(i).

Suggestions welcome!

\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 76

/* Answer below. For testing purpose, redirect consoloe.log */ console.log=x=>document.write(x+' ')

for(i=0;i++<1e7;)/0|(.).*\1/.test(i)||[...i+''].some(d=>i%d)||console.log(i)

The regexp test for 0 or repeated digits. Then the digits array is checked looking for a non-zero modulo for any digit.

here is the explanation of the 7 digit max.

\$\endgroup\$
3
\$\begingroup\$

Ruby, 130 bytes

... not counting whitespace

New to programming,just wanted to participate

c=0
(0..10**7).each do |x| 
  a=x.to_s.split('')
  c+=1 if !a.include?('0')&& a.uniq!.eql?(nil)&&a.all?{|y| x.modulo(y.to_i).zero?} 
end
p c
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to PPCG! Check out some additional Tips for Ruby to help get that code length down. \$\endgroup\$ – AdmBorkBork Sep 30 '15 at 2:43
3
\$\begingroup\$

C, 122 bytes

i,j,m,a;void f(){for(i=1;i<1e8;++i){for(m=0,j=i;j;j/=10){a=j%10;if(!a||m&(1<<a)||i%a)goto n;m|=1<<a;}printf("%d ",i);n:;}}

Prettier:

i,j,m,a;
void f()
{
    for (i=1; i<1e8; ++i){
        for (m=0, j=i;  j;  j/=10) {
            a = j%10;
            if (!a || m&(1<<a) || i%a)
                goto n;
            m|=1<<a;
        }
        printf("%d ",i);
    n:;
    }
}

For each candidate i, we iterate its digits a in little-endian order, keeping track of seen digits in the bits of m. If the loop completes, then all digits are factors of i and we saw no zeros or repeated digits, so print it, otherwise we exit early to continue the outer loop.

\$\endgroup\$
  • \$\begingroup\$ Good to the the goto command being used. \$\endgroup\$ – Shaun Bebbers May 7 at 14:39
2
\$\begingroup\$

CJam, 34 bytes

1e7{_:TAb___&=\{T\T)e|%}%:+!**},N*
\$\endgroup\$
2
\$\begingroup\$

Lua, 129 bytes

I've eschewed the string approach for pure digit-crunching, which seems a bit speedier and probably saved me some bytes as well. (I'll have test that theory, but Lua string handling is pretty verbose compared to some other languages.)

for i=1,1e7 do t={[0]=1}j=i while j>0 do c=j%10 if t[c]or i%c>0 then break end t[c]=1 j=(j-c)/10 if j==0 then print(i)end end end
\$\endgroup\$
2
\$\begingroup\$

gawk, 99 bytes

BEGIN{for(;8>(l=split(++i,a,_));printf f?f=_:i RS)for(j in a)f=i~0||i%(d=a[j])||i-d*10^(l-j)~d?1:f}

I could reduce that to 97 if I would use END instead of BEGIN, but then you would have to press Ctrl-D to start the actual output, signalling that there will be no input.

I could reduce it to even 94 if I would write nothing instead of BEGIN or END, but then you would have to press the return key once to start it, which could be counted as input.

It simply goes over the digits of each number and tests if the criteria are met.

i~0               :  number contains a `0`?                          -> trash
i%(d=a[j])        :  number not divisible by current digit?          -> trash
i-d*10^(l-j)~d    :  I removed the current digit from the number yet it
                  :  still contains it?                              -> trash

Takes 140 seconds to terminate on my Core 2 Duo.

\$\endgroup\$
2
\$\begingroup\$

Jelly, 11 bytes

9œ!ṖẎgḌ$ƑƇḌ

This uses the two-week old œ! atom. Actually fast enough to run on TIO.

Try it online!

How it works

9œ!ṖẎgḌ$ƑƇḌ  Main link. No arguments.

9            Set the return value to 9.
   R         Pop; yield [1, ..., 8].
 œ!          Promote 9 to [1, ..., 9] and generate all permutations of length k,
             each k in the right argument [1, ..., 8].
    Ẏ        Tighten; dump all digit lists in a single array.
         Ƈ   Comb; keep only digit lists for which the link to the left returns 1.
        Ƒ      Fixed; return 1 iff calling the link to the left returns its argument.
       $         Combine the two links to the left into a monadic chain.
      Ḍ            Undecimal; convert the digit list into an integer.
     g             Take the GCD of each digit and the integer.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.