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>
Respuestas:
Jalea , 8 bytes
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
fuente
Python 2, 85 bytes
Prints a list.
I'm basically combining two of my answers to previous challenges:
Checking if a number is divisible by each of its digits
(thanks to FryAmTheEggman for reminding me about this).
Determine if all decimal digits are unique
Thanks to xsot for 1 byte saved by combining the conditions better.
fuente
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`)]
Perl,
6147 bytes46 bytes code + 1 byte command line parameter.
Usage:
Explanation
/(.).*\1|0/
returns 1 if the number-under-test contains a duplicate character or a 0s/./$_%$&/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.fuente
say
instead ofprint
+-l
:-)say
required an explicit declaration first?use feature 'say'
oruse 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 :)map
andsay
gets this down to 43: Try it online!Pyth,
2221Thanks 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
to10^7-1
which covers all the necessary values. This version may cause a memory error if it cannot make the listS^T7
, which is similar tolist(range(1,10**7))
in python 3 (However, it works fine for me). If so, you could try:Which finds the first 548 Monday numbers. This also demonstrates another way to check for the
0
s 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)fuente
.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.55, 66, 77, 88, 99
, all numbers with duplicated digits...{
having been changed, since replacing it with{I
seems to work.GS2,
2019 bytesgs2 uses a wide range of bytes, not just printable ascii chracters. I will present my solution in hex.
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)
fuente
Python 3,
132128114111104 bytesThere are 548 Monday Numbers.
fuente
1e8
instead of even9**9
?'0' not
. Also,i%int(k)==0
can probably bei%int(k)<1
?j=`i`
.if len(set(j))+2==len(j)+('0'in j)+all(i%int(k)<1 for k in j)
APL,
443937 bytesUngolfed:
Saved 7 bytes thanks to Moris Zucca!
fuente
TI-BASIC,
6059 bytes∟D
is the list of digits, which is generated using math and therandIntNoRep(
command (random permutation of all integers between1
and1+int(log(X
inclusive). I use a slightly complicated chain of statements to check if all of the conditions are satisfied:To fail numbers that have repeated digits or zero digits, I replace zeroes with
2X
, becauseX
is never divisible by2X
.To special-case 1~9 (because
ΔList(
on a one-element list errors) I use theIf
statement in the fourth line to skip over the check in the fifth line, automatically displaying allX
≤9.The output numbers are separated by newlines.
fuente
Mathematica 105
IntegerDigits
breaks upn
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 ofn
.fuente
9^9
or1e8
or somethingUnion
to check for duplicates.Haskell, 77 bytes
Usage example (the first 20 numbers):
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.fuente
R, 99 bytes
Slightly less golfed:
fuente
Perl,
907570 bytesfuente
^
and the$
around the0
in yourgrep
, you can replace the&&
befores/./
with a single&
and I think the last|0
is unneeded (although only tested up-to1e3
...). Well and truly thrashed my score! :)CJam, 25 bytes
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:
fuente
C#,
230227It'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:
Ungolfed:
fuente
TI-BASIC,
5553 bytesThis 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.
My main change is from
randIntNoRep(1,
torandIntNoRep(0,
meaning that there will now a zero in every generated list of digits.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(
toXnot(
. 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 amin(Δ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.fuente
05AB1E,
302221181413129 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
Try it online (only outputs the numbers below 103 instead of 107 to prevent a timeout after 60 sec).
Explanation:
Previous 12 byter version (one of my very first 05AB1E answers):
NOTE: Only works in the legacy version of 05AB1E.
Try it online (only outputs the numbers below 103 instead of 107 to prevent a timeout after 60 sec).
Explanation:
fuente
297
, which is not in the sequence of Lynch-Bell numbers.7°LʒÐÑÃÙQ
Julia, 88 bytes
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:
fuente
Python 2, 101 bytes
You can omit the
print
in the interpreter get to 96. Used6**9
since it is 8 digits while the largest monday number is only 7 digits, something like9**9
would probably take a long time, 6**9 only takes about 10 seconds.fuente
1e7
is a float, range takes integers.Perl, 97 bytes
Takes a while to run, but produces the required output, change to
1e3
for a quicker example!fuente
y///c==grep{2>eval"$n=~y/$_//"}/./g
, could you use something along the lines of!/(.).*\1/
?MATLAB, 100
And in a more readable format:
Basically this counts through every number between1 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:
First check if there are any duplicates. By sorting the array, if the difference between any consecutive digits is zero, then there are duplicates
Check if there are any zeros. The ASCII for 0 is 48, so we check that all digits are not equal to that.
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.
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:
Which if you run this code with that as the input:
Yeilds 548.
fuente
Ruby, 79
More interesting but slightly longer solution with a regex:
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 to1.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.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]/
.fuente
digits
function on integers, you can save bytes from the eval hack since you aren't operating on strings any more! 63 bytes.Pip, 25 bytes
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:
The
#=
operator compares two iterables by length. If the number ofU
niQ
ue characters ina
is the same as the number of characters ina
, 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:
fuente
Couple hours later
It's a good thing performance isn't taken into account.Javascript (ES6),
1069083 bytesKids, 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.
The first regex (props to @Jarmex) returns
true
if the number contains duplicate digits or zeroes. If this turns outfalse
, the program move on to the second, which replaces each digitj
withi%j
. The result is all zeroes if it's divisible by all of it's digits, in which case it moves on toconsole.log(i)
.Suggestions welcome!
fuente
JavaScript (ES6), 76
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.
fuente
Ruby, 130 bytes
... not counting whitespace
New to programming,just wanted to participate
fuente
C, 122 bytes
Prettier:
For each candidate
i
, we iterate its digitsa
in little-endian order, keeping track of seen digits in the bits ofm
. If the loop completes, then all digits are factors ofi
and we saw no zeros or repeated digits, so print it, otherwise we exit early to continue the outer loop.fuente
goto
command being used.CJam, 34 bytes
fuente
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.)
fuente
gawk, 99 bytes
I could reduce that to 97 if I would use
END
instead ofBEGIN
, 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
orEND
, 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.
Takes 140 seconds to terminate on my Core 2 Duo.
fuente
Jelly, 11 bytes
This uses the two-week old
œ!
atom. Actually fast enough to run on TIO.Try it online!
How it works
fuente