Generar números de lunes


35

Los números de los lunes , según lo definido por Gamow en esta pregunta sobre Puzzling, son enteros positivos N con las siguientes tres propiedades:

  • La representación decimal de N no contiene el dígito 0
  • La representación decimal de N no contiene ningún dígito dos veces
  • N es divisible por cada dígito D que aparece en su representación decimal

Tenga en cuenta que estos se conocen alternativamente, en el OEIS, como números de Lynch-Bell .

Ejemplos:

  • 15es un número lunes, ya que es divisible por tanto 1y 5y satisface las otras dos condiciones
  • 16no lo es, porque no es divisible por 6.
  • El número 22no lo es, porque aunque satisface las condiciones 1 y 3, falla la condición 2.

Aquí está la lista de los primeros 25 números de lunes para comenzar (hay 548 en 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

El desafío aquí es escribir el código más corto que genere la secuencia completa de números de lunes, desde 1 hasta 9867312 (demostrado en esa pregunta como el más grande posible).

Su código no debe tener entrada, y la salida debe ser STDOUT o equivalente, con su elección de delimitador. Se aplican todas las reglas habituales de código de golf y están prohibidas las lagunas estándar .

Tabla de clasificación



1
También conocidos como números de Lynch-Bell .
Geobits

@Geobits Gracias - No pude encontrarlo en OEIS por alguna razón.
AdmBorkBork

8
Usted debería haber publicado este desafío de ayer ...
mbomb007

2
@ mbomb007 Sin embargo, ¡no vi la pregunta de Gamow hasta esta mañana!
AdmBorkBork

Respuestas:


1

Jalea , 8 bytes

ȷ7Dg⁼QƲƇ

Se ejecuta localmente en menos de ocho minutos.

Pruébalo en línea! (modificado para encontrar números con seis dígitos o menos)

Cómo funciona

ȷ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.

16

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.


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`)]
xsot

11

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.


Very nice. You can save a few bytes by making it Perl 5.10 and using say instead of print + -l :-)
xebtl

Thanks for the suggestion! I thought say required an explicit declaration first?
Jarmex

@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 :)
hobbs

2
@hobbs This answer on meta says “The consensus up to now on SO and here was "the -M5.010, when needed, is free"”.
xebtl

2
Using map and say gets this down to 43: Try it online!
Xcali

10

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)


1
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.
Jakube

1
@Jakube Backticks are a pain in comments aren't they? :P Thanks a lot though!
FryAmTheEggman

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..
Kevin Cruijssen

1
@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.
FryAmTheEggman

@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. :)
Kevin Cruijssen

9

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

8

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.


1
Could you use 1e8 instead of even 9**9?
Dom Hastings

Remove the space in '0' not. Also, i%int(k)==0 can probably be i%int(k)<1?
mbomb007

Thanks. I didn't mean to add that back in. @mbomb007
Zach Gates

You can use j=`i`.
mbomb007

For another -6 use if len(set(j))+2==len(j)+('0'in j)+all(i%int(k)<1 for k in j)
lirtosiast

7

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!


I love APL. This is why.
Conor O'Brien

I think you can golf it using function trains, saving 5 bytes: {0=+/(⊢|∘⍵,0∘∊,⍴∘⊢≠⍴∘∪)x←⍎¨⍕⍵:⍵⋄⍬}¨⍳1e7
Moris Zucca

@MorisZucca Awesome, thanks for the suggestion!
Alex A.

I just saw that in this form x← is not needed any more, so 2 more bytes saved! :-)
Moris Zucca

@MorisZucca You're an APL golfing machine! Thanks again!
Alex A.

6

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.


5

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


I expect there is a way in mathematica to get large numbers in fewer bytes, like 9^9 or 1e8 or something
FryAmTheEggman

I'm surprised Mathematica doesn't have a built-in for this ;-). Nice trick with the Union to check for duplicates.
AdmBorkBork

@FryAmTheEggman, You are correct about Mathematica allowing for 9^9. But wouldn't that return more than 548 Monday numbers?
DavidC

As said in the question, there is no possible Monday number greater than the one given as the upper limit.
FryAmTheEggman

5

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.


5

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,"")
    }

5

Perl, 90 75 70 bytes

print+($_,$/)x(grep(!/(\d).*\1|0/,$_)&s/./!$&||$_%$&/ger<1)for 1..1e7

1
Ahhh, I missed the \1 trick for dupe checking, nice. Can you save more with a statement modifier while and a ternary print?
Dom Hastings

@DomHastings thanks, now more golfed using your suggestion
steve

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! :)
Dom Hastings

1
@DomHastings thanks, down to 70 with your golfing tips.
steve

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!
Xcali

4

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.

4

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


can (int)1e7 be 1<<24?
lirtosiast

@ThomasKwa Yes, it can be. Indeed. Thanks!
RobIII

4

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.


4

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)

Your answer prints 297, which is not in the sequence of Lynch-Bell numbers.
Mr. Xcoder

@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
Kevin Cruijssen

1
9 bytes: 7°LʒÐÑÃÙQ
Grimmy

@Grimy One of my very first 05AB1E answers. :) Nice approach!
Kevin Cruijssen

3

Julia, 88 bytes

print(join(filter(i->(d=digits(i);0d&&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, " "))

3

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.


As pointed out on a couple of questions 1e7 is shorter than both
Holloway

@Trengot 1e7 is a float, range takes integers.
Rohcana

Very true. Hadn't thought of that
Holloway

3

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!


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/?
msh210

@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!
Dom Hastings

3

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×107 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.


3

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]/.


Now that Ruby 2.4 has a digits function on integers, you can save bytes from the eval hack since you aren't operating on strings any more! 63 bytes.
Value Ink

3

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)

This is a pretty neat language! Nice to see something other than stack-based golfing.
AdmBorkBork

1
@TimmyD If you want to see non-stack based golfing, there tends to be quite a bit of Pyth around.
Reto Koradi

@RetoKoradi But if you want to see non-stack-based golfing with infix operators, Pip is for you. ;^)
DLosc

Couple hours later It's a good thing performance isn't taken into account.
Holloway

3

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!


3

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.


3

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

2
Welcome to PPCG! Check out some additional Tips for Ruby to help get that code length down.
AdmBorkBork

3

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.


Good to the the goto command being used.
Shaun Bebbers

2

CJam, 34 bytes

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

2

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

2

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.


2

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.
Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.