This challenge is simple enough that it's basically all in the title: you're given a positive integer N and you should return the smallest positive integer which is not a divisor of N.
An example: the divisors of N = 24 are 1, 2, 3, 4, 6, 8, 12, 24
. The smallest positive integer which is not in that list is 5, so that's the result your solution should find.
This is OEIS sequence A007978.
Rules
You may write a program or a function and use any of the our standard methods of receiving input and providing output.
You may use any programming language, but note that these loopholes are forbidden by default.
This is code-golf, so the shortest valid answer – measured in bytes – wins.
Test Cases
The first 100 terms are:
2, 3, 2, 3, 2, 4, 2, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 4, 2, 3, 2, 3, 2, 5, 2,
3, 2, 3, 2, 4, 2, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 4, 2, 3, 2, 3, 2, 5, 2, 3,
2, 3, 2, 4, 2, 3, 2, 3, 2, 7, 2, 3, 2, 3, 2, 4, 2, 3, 2, 3, 2, 5, 2, 3, 2,
3, 2, 4, 2, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 4, 2, 3, 2, 3, 2, 5, 2, 3, 2, 3
In particular, make sure that your answer works for inputs 1 and 2 in which case the result is larger than the input.
And for some larger test cases:
N f(N)
1234567 2
12252240 19
232792560 23
Leaderboard
Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.
/* Configuration */
var QUESTION_ID = 105412; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 48934; // This should be the user ID of the challenge author.
/* App */
var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;
function answersUrl(index) {
return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}
function commentUrl(index, answers) {
return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}
function getAnswers() {
jQuery.ajax({
url: answersUrl(answer_page++),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
answers.push.apply(answers, data.items);
answers_hash = [];
answer_ids = [];
data.items.forEach(function(a) {
a.comments = [];
var id = +a.share_link.match(/\d+/);
answer_ids.push(id);
answers_hash[id] = a;
});
if (!data.has_more) more_answers = false;
comment_page = 1;
getComments();
}
});
}
function getComments() {
jQuery.ajax({
url: commentUrl(comment_page++, answer_ids),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
data.items.forEach(function(c) {
if (c.owner.user_id === OVERRIDE_USER)
answers_hash[c.post_id].comments.push(c);
});
if (data.has_more) getComments();
else if (more_answers) getAnswers();
else process();
}
});
}
getAnswers();
var SCORE_REG = /<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;
var OVERRIDE_REG = /^Override\s*header:\s*/i;
function getAuthorName(a) {
return a.owner.display_name;
}
function process() {
var valid = [];
answers.forEach(function(a) {
var body = a.body;
a.comments.forEach(function(c) {
if(OVERRIDE_REG.test(c.body))
body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
});
var match = body.match(SCORE_REG);
if (match)
valid.push({
user: getAuthorName(a),
size: +match[2],
language: match[1],
link: a.share_link,
});
});
valid.sort(function (a, b) {
var aB = a.size,
bB = b.size;
return aB - bB
});
var languages = {};
var place = 1;
var lastSize = null;
var lastPlace = 1;
valid.forEach(function (a) {
if (a.size != lastSize)
lastPlace = place;
lastSize = a.size;
++place;
var answer = jQuery("#answer-template").html();
answer = answer.replace("{{PLACE}}", lastPlace + ".")
.replace("{{NAME}}", a.user)
.replace("{{LANGUAGE}}", a.language)
.replace("{{SIZE}}", a.size)
.replace("{{LINK}}", a.link);
answer = jQuery(answer);
jQuery("#answers").append(answer);
var lang = a.language;
if (/<a/.test(lang)) lang = jQuery(lang).text();
languages[lang] = languages[lang] || {lang: a.language, user: a.user, size: a.size, link: a.link};
});
var langs = [];
for (var lang in languages)
if (languages.hasOwnProperty(lang))
langs.push(languages[lang]);
langs.sort(function (a, b) {
if (a.lang > b.lang) return 1;
if (a.lang < b.lang) return -1;
return 0;
});
for (var i = 0; i < langs.length; ++i)
{
var language = jQuery("#language-template").html();
var lang = langs[i];
language = language.replace("{{LANGUAGE}}", lang.lang)
.replace("{{NAME}}", lang.user)
.replace("{{SIZE}}", lang.size)
.replace("{{LINK}}", lang.link);
language = jQuery(language);
jQuery("#languages").append(language);
}
}
body { text-align: left !important}
#answer-list {
padding: 10px;
width: 290px;
float: left;
}
#language-list {
padding: 10px;
width: 290px;
float: left;
}
table thead {
font-weight: bold;
}
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>
-
\$\begingroup\$ I turned the sample output string into a vector of numbers, and realized that if you format it 24 columns across, it's extremely repetitive, except for the odd deviation. \$\endgroup\$Carcigenicate– Carcigenicate2017年01月06日 00:05:08 +00:00Commented Jan 6, 2017 at 0:05
-
1\$\begingroup\$ That makes sense, 24 is 0 mod 2, 3, and 4, so the only differences would be in columns where the numbers are >4. It's even more repetitive at width 120. \$\endgroup\$CalculatorFeline– CalculatorFeline2018年01月31日 04:41:03 +00:00Commented Jan 31, 2018 at 4:41
103 Answers 103
Mathematica, 19 bytes (UTF-8 encoding)
1//.x_/;x∣#:>x+1&
Unnamed function taking a nonzero integer argument and returning a positive integer. The vertical bar about halfway through is actually the three-byte character U+2223, which denotes the divisibility relation in Mathematica. Explanation:
1 Starting with 1,
//. apply the following rule until it stops mattering:
x_ if you see a number x
/;x∣# such that x divides the function argument,
:>x+1 replace it with x+1.
& Cool, that's a function.
Edited to add: ngenisis points out that //.
will, by default, iterate a maximum of 65536 times. So this implementation works for all input numbers less than the least common multiple of the integers from 1 to 65538 (in particular, on all numbers with at most 28436 digits), but technically not for all numbers. One can replace x//.y
with ReplaceRepeated[x,y,MaxIterations->∞]
to fix this flaw, but obviously at the cost of 34 additional bytes.
-
\$\begingroup\$ Very interesting way to loop without using
For
,While
, etc \$\endgroup\$user61980– user619802017年01月03日 17:07:25 +00:00Commented Jan 3, 2017 at 17:07 -
6\$\begingroup\$ I learned it from this site! I'm definitely enjoying learning more about Mathematica by being here (can I justify that on my timesheet...?). \$\endgroup\$Greg Martin– Greg Martin2017年01月03日 17:49:48 +00:00Commented Jan 3, 2017 at 17:49
-
3\$\begingroup\$ That does not look like mathematica O_o \$\endgroup\$Mama Fun Roll– Mama Fun Roll2017年01月03日 19:14:34 +00:00Commented Jan 3, 2017 at 19:14
-
2\$\begingroup\$ don't let the lack of capital letters and brackets fool ya ;) \$\endgroup\$Greg Martin– Greg Martin2017年01月03日 20:22:44 +00:00Commented Jan 3, 2017 at 20:22
-
2\$\begingroup\$ Actually only 28436 digits. \$\endgroup\$user202729– user2027292018年02月22日 07:20:08 +00:00Commented Feb 22, 2018 at 7:20
JavaScript (ES6), (削除) 25 (削除ここまで) 23 bytes
f=(n,k)=>n%k?k:f(n,-~k)
Note: One interesting thing here is that the k
parameter is initialized ex nihilo on the first iteration. This works because n % undefined
is NaN
(falsy as expected) and -~undefined
equals 1
. On the next iterations, -~k
is essentially equivalent to k+1
.
Test
f=(n,k)=>n%k?k:f(n,-~k)
// first 100 terms
for(i = 1, list = []; i <= 100; i++) {
list.push(f(i));
}
console.log(list.join(' '));
// larger test cases
console.log(f(1234567));
console.log(f(12252240));
console.log(f(232792560));
-
\$\begingroup\$ Exactly what I got. I'd be surprised if anything shorter is possible \$\endgroup\$ETHproductions– ETHproductions2017年01月03日 20:25:49 +00:00Commented Jan 3, 2017 at 20:25
-
\$\begingroup\$ @ETHproductions On second thought, there's a shorter one. :-) \$\endgroup\$Arnauld– Arnauld2017年01月03日 20:31:40 +00:00Commented Jan 3, 2017 at 20:31
-
7\$\begingroup\$ Um. That's... uh... wow. \$\endgroup\$ETHproductions– ETHproductions2017年01月03日 20:33:52 +00:00Commented Jan 3, 2017 at 20:33
Pyth, 3 bytes
f%Q
Basically, f
loops the code until %QT
(Q % T
where T
is the iteration variable) is true.
-
2\$\begingroup\$ Saw the problem, made this answer, came here to post it, found yours. Well done! \$\endgroup\$izzyg– izzyg2017年01月04日 10:08:38 +00:00Commented Jan 4, 2017 at 10:08
-
\$\begingroup\$ I wrote this and felt awesome about myself:
.V1In%Qb0bB
Saw your answer, and not feeling so awesome anymore. \$\endgroup\$John Red– John Red2017年01月05日 10:28:41 +00:00Commented Jan 5, 2017 at 10:28 -
\$\begingroup\$ @JohnRed Lol, I think you just need to familiarize yourself with the built-ins in Pyth. \$\endgroup\$busukxuan– busukxuan2017年01月05日 10:34:07 +00:00Commented Jan 5, 2017 at 10:34
-
2\$\begingroup\$ That's a very impressive score for Hexagony, nice work! \$\endgroup\$Martin Ender– Martin Ender2017年01月03日 09:51:06 +00:00Commented Jan 3, 2017 at 9:51
Python, (削除) 43 (削除ここまで) (削除) 36 (削除ここまで) 35 bytes
f=lambda n,d=2:d*(n%d>0)or f(n,d+1)
R, 28 bytes
Pretty straightforward, nothing fancy. Takes input from stdin, increments value T
until i
modulo T
is nonzero.
i=scan()
while(!i%%T)T=T+1
T
If you want something a little more fancy, there's the following for 29 bytes:
i=scan()
match(0,!i%%1:(i+1))
Explained:
i=scan()
: Readi
from stdin.
1:(i+1)
: Generate all integers from1
toi+1
(the+1
accounting for the cases of1
and2
).
i%%1:(i+1)
: Modulo the input by every number in our list.
!i%%1:(i+1)
: Negate the resulting list; this implicitly converts it to a logical type, such that0
isFALSE
and nonzero isTRUE
. After negating,TRUE
values becomeFALSE
and vice-versa. Now, all originally nonzero values are coded asFALSE
.
match(0,!i%%1:(i+1))
: Return the index of the first instance of0
in our list.0
isFALSE
, so this returns the index of the firstFALSE
in the list, which is the first nonzero value from the modulo operation. Since our original list began at1
, the index is equal to the value of the smallest non-divisor.
-
\$\begingroup\$ Nice, just wanted to suggest using
which.min
, but then I saw the edit and it seemsmatch
does a similar job. \$\endgroup\$JAD– JAD2017年01月03日 10:34:43 +00:00Commented Jan 3, 2017 at 10:34 -
2\$\begingroup\$ Also, nice trick using
T
, saving the need to define it before thewhile
loop. \$\endgroup\$JAD– JAD2017年01月03日 10:36:05 +00:00Commented Jan 3, 2017 at 10:36 -
\$\begingroup\$ @JarkoDubbeldam Thanks! I can't find a way for the vectorized approach to be shorter than the
while
approach, which is fine as it's very memory-intensive for large N. TheT
trick is one of those treats which is great for golfing but absolutely horrible for actual programming. (And of course you can useF
too when you need a0
.) \$\endgroup\$rturnbull– rturnbull2017年01月03日 10:40:13 +00:00Commented Jan 3, 2017 at 10:40 -
\$\begingroup\$ You may save two bytes by using 0:i+1 instead of 1:(i+1) although I am not sure how it plays with the %% operator. \$\endgroup\$asac– asac2017年01月04日 17:52:09 +00:00Commented Jan 4, 2017 at 17:52
-
\$\begingroup\$ @antoine-sac Unfortunately,
%%
takes precedence over+
, so parens are still necessary:(0:i+1)
, with the same number of bytes as1:(i+1)
. I actually had the former originally, but changed it to the latter as it's easier to read. \$\endgroup\$rturnbull– rturnbull2017年01月05日 00:14:16 +00:00Commented Jan 5, 2017 at 0:14
Haskell, 26 bytes
f n=until((>0).mod n)(+1)1
Everyone forgets about until
!
COW, 174 bytes
oomMOOMMMmoOmoOmoOMMMmOomOoMoOMMMmoOmoOmoOMMMmOoMOOmoO
MOomoOMoOmOoMOOmoOmoomoOMOOmOoMoOmoOMOomoomOomOoMOOmOo
moomoOMOomoomoOmoOMOOmOomOomOomOoOOMOOOMOomOOmoomOomOo
mOomOomOomoo
This code is only partially my own -- it implements a modulus algorithm that I ported from brainfuck. The rest of the code is my own. However, since I did not write the modulus algorithm, I haven't truly investigated how it works and cannot document that part of the code. Instead, I'll give my usual breakdown, followed by a more in-depth explanation of why the code works.
Code breakdown
oom ;Read input into [0].
MOO ;Loop while [0]. We never change [0], so the program only terminates forcibly after a print.
MMMmoOmoOmoOMMMmOomOo ; Copy [0] to [3] and navigate to [1].
MoOMMMmoOmoOmoOMMM ; Increment [1], and copy it to [4]
mOo ; Navigate back to [3].
MOO ; Modulus algorithm. Direct port of brainfuck algorithm.
moOMOomoOMoOmOo
MOO
moO
moo
moO
MOO
mOoMoOmoOMOo
moo
mOomOo
MOO
mOo
moo
moOMOo
moo ; End modulus algorithm.
moOmoO ; Navigate to [5]. This contains our modulus.
MOO ; Only perform these operations if [5] is non-zero -- i.e. [0] % [1] != 0
mOomOomOomOoOOMOOOMOomOO ; Navigate to [1], print its contents, then error out.
moo ; End condition
mOomOomOomOomOo ; Since we're still running, [0] % [1] == 0, so navigate back to [0] and try again.
moo ;End main loop.
Explanation
The code first reads the integer into [0]. Each iteration of the main loop (lines 2 through 26) increments [1], then copies everything necessary over to the modulus algorithm, which spits out its result into [5]. If [5] contains any value, then [1] is the number we need to print. We print it, and then force-quit the program.
Since COW is a brainfuck derivative, it functions relatively similar to the way brainfuck operates -- infinite strip of tape, you can move left or right, increase or decrease, and "loop" while the current tape value is non-zero. In addition to brainfuck, COW comes with a couple of useful features.
(0) moo -- Equivalent to ]
(1) mOo -- Equivalent to <
(2) moO -- Equivalent to >
(3) mOO -- No equivalent. Evaluate current tape value as instruction from this list.
(4) Moo -- If tape is 0, equivalent to ,; if tape is non-zero, equivalent to .
(5) MOo -- Equivalent to -
(6) MoO -- Equivalent to +
(7) MOO -- Equivalent to [
(8) OOO -- No equivalent. Set tape (positive or negative) to 0
(9) MMM -- No equivalent. If register is empty, copy tape to register. If register is non-empty, paste register to tape and clear register.
(10) OOM -- No equivalent. Print an integer from tape to STDOUT
(11) oom -- No equivalent. Read an integer from STDIN and store it on tape
The real point of interest here is instruction 3, mOO
. The interpreter reads the current tape value, and executes an instruction based on that tape value. If the value is less than 0, greater than 11, or equal to 3, the interpreter terminates the program. We can use this as a quick-and-dirty force quit of the main loop (and the program entirely) once we've found our non-divisor. All we have to do is print our number, clear [1] (with OOO
), decrement it to -1 with MOo
, and then execute instruction -1 via mOO
which ends the program.
The tape itself for this program functions as follows:
[0] -- Read-in integer from STDIN.
[1] -- Current divisor to test
[2] -- Placeholder for modulus algorithm
[3] -- Temporary copy of [0] for use for modulus algorithm
[4] -- Temporary copy of [1] for use for modulus algorithm
[5] -- Placeholder for modulus algorithm. Location of remainder at end of loop.
[6] -- Placeholder for modulus algorithm
[7] -- Placeholder for modulus algorithm
The modulus algorithm naturally clears [2], [3], [6], and [7] at the end of the operation. [4]'s contents get overwritten with the register paste on line 4, and [5] is zero when [0] is divisible by [1], so we don't have to clear it. If [5] is non-zero, we force-quit on line 23 so we don't have to worry about it.
Brachylog, 10 bytes
~{=#>:A'*}
This came out very similar to (but shorter than) Fatalize's original solution. Fatalize has since switched to a different algorithm that ties with this one via a different method, so I'm going to have to explain it myself:
~{=#>:A'*}
~{ } inverse of the following function:
= try possible values for the input, if it's unbound
#> the input is a positive integer
:A'* there is no A for which the input times A is the output
When we invert the function, by swapping "input" and "output", we get a fairly reasonable algorithm (just expressed in an awkward way): "try possible positive integers, in their natural order (i.e. 1 upwards), until you find one that can't be multiplied by anything to produce the input". Brachylog doesn't do floating-point calculations unless all inputs are known, so it'll only consider integer A.
-
1\$\begingroup\$ Never thought about doing that, that's neat! \$\endgroup\$Fatalize– Fatalize2017年01月03日 10:38:58 +00:00Commented Jan 3, 2017 at 10:38
Brachylog, (削除) 11 (削除ここまで) 10 bytes
,.=:?r'%0'
Explanation
,.= Assign an integer to the output
. :?r'%0 Input mod Output ≠ 0
0' Output ≠ 0
05AB1E, 7 bytes
Xμ1NÖ_1⁄2
Explanation
Xμ # run until counter is 1
1 # push input
N # push iteration counter
Ö_ # push input % iteration counter != 0
1⁄2 # if true, increase counter
# output last iteration
-
\$\begingroup\$ Nice, I was wondering how you'd do this iteratively in 05AB1E. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年01月03日 15:36:23 +00:00Commented Jan 3, 2017 at 15:36
Jelly, 5 bytes
1%@#Ḣ
Explanation:
1%@#Ḣ
1 # Find the first ... numbers, counting up from 1, such that
%@ dividing those numbers into ... gives a truthy remainder
Ḣ then return the first
This is a horrendous abuse of #
; there are plenty of operators in this program, but a ton of missing operands. #
really wants the 1
to be given explicitly for some reason (otherwise it tries to default to the input); however, everything else that isn't specified in the program defaults to the program's input. (So for example, if you give 24 as input, this program finds the first 24 numbers that don't divide 24, then returns the first; kind-of wasteful, but it works.)
-
\$\begingroup\$ Damn you Jelly! Pyth beats you today! :D \$\endgroup\$John Red– John Red2017年01月05日 10:38:40 +00:00Commented Jan 5, 2017 at 10:38
-
\$\begingroup\$ ASCII-only:
2%@1#
\$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年01月30日 20:08:20 +00:00Commented Jan 30, 2018 at 20:08
C, (削除) 32 (削除ここまで) 35 bytes
i;f(x){for(i=1;x%++i<1;);return i;}
Edit: added i=1
in the loop
Usage
main(c,v)char**v;{printf("%d",f(atoi(*++v)));}
Full Program version, 64 Bytes:
main(c,v)char**v;{*++v;for(c=1;atoi(*v)%++c<1;);printf("%d",c);}
C#, (削除) 39 (削除ここまで) 37 bytes
n=>{int i=0;while(n%++i<1);return i;}
Saved two bytes thanks to Martin!
-
\$\begingroup\$ I like while(!(n%++i)); better but of course, this is code golf and 1 byte is 1 byte. \$\endgroup\$John Hamilton– John Hamilton2017年01月03日 12:57:19 +00:00Commented Jan 3, 2017 at 12:57
-
\$\begingroup\$ Does that work? I didn't know that the 0 evaluated to false automatically \$\endgroup\$Alfie Goodacre– Alfie Goodacre2017年01月03日 15:40:53 +00:00Commented Jan 3, 2017 at 15:40
-
\$\begingroup\$ Ah, I tried it in C++, yeah it doesn't work with C#. \$\endgroup\$John Hamilton– John Hamilton2017年01月04日 07:31:28 +00:00Commented Jan 4, 2017 at 7:31
05AB1E, 6 bytes
ÌL1ÑK¬
Also, it spells "LINK!"... Kinda...
ÌL # Push [1..n+2]
1Ñ # Push divisors of n.
K¬ # Push a without characters of b, and take first item.
-
\$\begingroup\$ @Zgarb missed that part, initial increment by 2 fixes the problem. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年01月03日 16:08:56 +00:00Commented Jan 3, 2017 at 16:08
-
1\$\begingroup\$ Nice! I always forget that 05ab1e has a divisor function :) \$\endgroup\$Emigna– Emigna2017年01月03日 18:30:22 +00:00Commented Jan 3, 2017 at 18:30
-
\$\begingroup\$ I know this wasn't possible yet in the legacy version of 05AB1E, but it can be -1 now by using
∞
instead ofÌL
. :) I've also just posted an alternative 5-byter. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年01月22日 14:09:45 +00:00Commented Jan 22, 2020 at 14:09
Jelly, 5 bytes
‘ḍ€i0
How it works
‘ḍ€i0 Main link. Argument: n
‘ Increment; yield n+1.
ḍ€ Divisible each; test 1, ..., n+1 for divisibility by n.
i0 Find the first index of 0.
Octave/MATLAB, (削除) 26 (削除ここまで) 24 bytes
@(n)find(mod(n,1:n+1),1)
find(...,1)
returns the index (1
-based) of the first nonzero element of the vector in the first argument. The first argument is [n mod 1, n mod 2, n mod 3, n mod 4,...,n mod (n+1)]
That means we have to add +1
to the index, since we start testing at 1
. Thanks @Giuseppe for -2 bytes.
Jelly, 6 bytes
%R;‘TḢ
Explanation:
Assume 24 is our N
R Generate all numbers from 1 to N [1, 2, 3, 4 .., 24]
;‘ Attach N+1 to that list (for cases 1,2) [1, 2, 3, 4 .., 25]
% And modulo-divide our input by it
Yields a list with the remainder [0, 0, 0, 0, 4 ...]
T Return all thruthy indexes [5, 7, ...]
Ḣ Takes the first element of that list --> 5
-
\$\begingroup\$ I don't know Jelly, but could you save a byte by increasing N before you generate the range? \$\endgroup\$Emigna– Emigna2017年01月03日 09:21:19 +00:00Commented Jan 3, 2017 at 9:21
-
\$\begingroup\$ @Emigna I don't know Jelly either ;) I don't see how: incrementing it earlier also makes the modulo test against N+1, or increaes the remainders
[1, 1, 1, 1, 5, ...]
. \$\endgroup\$steenbergh– steenbergh2017年01月03日 09:25:40 +00:00Commented Jan 3, 2017 at 9:25 -
\$\begingroup\$ Ah, I see. I thought it might be possible to do N%range(1,N+1), but if it increases the N in both instances that's no good. \$\endgroup\$Emigna– Emigna2017年01月03日 09:28:23 +00:00Commented Jan 3, 2017 at 9:28
Python 2.7.9, 32 bytes
f=lambda n,d=1:n%d>0or-~f(n,d+1)
Recursively counts up potential non-divisors d
. It's shorter to recursively the increment the result than to output d
. An offset of 1
is achieved by the Boolean of True
, which equals 1
, but since d==1
is always a divisor, the output is always converted to a number.
Python 2.7.9 is used to allow allow 0or
. Versions starting 2.7.10 will attempt to parse 0or
as the start of an octal number and given a syntax error. See this on Ideone.
Perl 6, 17 bytes
{first $_%*,1..*}
Expanded:
{ # bare block lambda with implicit parameter 「$_」
# return the first value
first
# where the block's argument 「$_」 modulus the current value 「*」
# doesn't return 0 ( WhateverCode lambda )
$_ % *,
# ( 「$_ !%% *」 would be the right way to write it )
# from 1 to Whatever
1 .. *
}
Perl, 19 bytes
18 bytes of code + -p
flag.
$_=$_%++$.?$.:redo
To run it:
perl -pE '$_=$_%++$.?$.:redo' <<< 12252240
Not very detailed explanations:
- $.
is a special variable whose default value is the current line number of the last filehandle accessed (stdin here), so after reading the first line of input, it's set to 1.
- $_
holds the input and is implicitly printed at the end (thanks to -p
flag).
- redo
(in that context) considers that the program is in a loop and redo the current iteration (only $.
will be different since it got incremented).
- So if we found the smallest number (stored in $.
) that doesn't divide $_
, then we set $_
to it, otherwise, we try the next number (thanks to redo
).
Actually, 7 bytes
;÷@uR-m
Try it online! (note: this is a very slow solution, and will take a long time for large test cases)
Explanation:
;÷@uR-m
;÷ duplicate N, divisors
@uR range(1, N+2)
- set difference (values in [1, N+1] that are not divisors of N)
m minimum
Haskell, 29 bytes
f n=[k|k<-[2..],mod n k>0]!!0
The expression [k|k<-[2..]]
just creates an infinite list [2,3,4,5,...]
. With the condition mod n k>0
we only allow those k
in the list that do not divide n
. Appending !!0
just returns the first entry (the entry at index 0
) form that list.
Dyalog APL, 8 bytes
1⍳⍨0≠⍳|⊢
1⍳⍨
position of first True in
0≠
the non-zero values of
⍳|
the division remainders of 1...N when divided by
⊢
N
Note: this works for 1 and 2 because 1⍳⍨
returns 1 + the length of its argument if none is found.
julia, 28 bytes
N->findfirst(x->N%x>0,1:N+2)
Note: since 1:N+2
doesn't allocate memory there is no memory problems for large N
s
- @flawr N+2
save for me some bytes
- @Martin 's suggestion saved 1 bytes
QBIC, 14 bytes
:[a+1|~a%b|_Xb
Explanation:
: Read the first cmd line param as a number, called 'a'
[a+1| FOR (b=1 ; b <= a+1; b++) <-- a+1 for cases a = 1 or 2
~a%b IF A modulo B ( == 0, implicit)
|_Xb THEN exit the program, printing b
[IF and FOR implicitly closed by QBIC]
C, 30 bytes
Recursion is your friend:
f(x,i){return x%i?i:f(x,i+1);}
Call as follows:
#include <stdio.h>
f(x,i){return x%i?i:f(x,i+1);}
int main() {
for(int i = 1; i < 10; i++) {
printf("f(%d, 1) = %d\n", i, f(i, 1));
}
}
-
\$\begingroup\$ I believe you have your ternary operator backwards.
x%i?i:f(x,i+1)
will callf(x,i+1)
whenx
is NOT divisible byi
instead of when it is. \$\endgroup\$Robert Benson– Robert Benson2017年01月05日 19:24:36 +00:00Commented Jan 5, 2017 at 19:24 -
\$\begingroup\$ @RobertBenson When
x
is divisible byi
,x%i
produces the value zero, which is interpreted as false by C, sof(x,i+1)
gets called. Thus, the recursion happens as long asx
is divisible byi
, just as it should. But I agree, this ternary expression is mind-screwing: I wrote it the wrong way round at the first try myself... \$\endgroup\$cmaster - reinstate monica– cmaster - reinstate monica2017年01月06日 09:35:35 +00:00Commented Jan 6, 2017 at 9:35 -
\$\begingroup\$ That's what I get for looking at these things while I'm trying to dig through
FORTRAN
code at work. Also... I might have looked at a different question just before looking at your answer... yeah... I'll use that as my excuse. :) Nice work. \$\endgroup\$Robert Benson– Robert Benson2017年01月07日 02:16:40 +00:00Commented Jan 7, 2017 at 2:16
PHP, 30 bytes
for(;$argv[1]%++$i<1;);echo$i;
if run from console with -r
option (thx to @ais523)
php -r 'for(;$argv[1]%++$i<1;);echo$i;' 232792560
32 bytes
<?for(;$argv[1]%++$i<1;);echo$i;
thanks to @manatwork for removing 1 byte
33 bytes (original)
<?for(;$argv[1]%++$i==0;);echo$i;
-
3\$\begingroup\$ IIRC, the
<?
doesn't have to be part of your byte count (because PHP has a command-line mode that doesn't require it). \$\endgroup\$user62131– user621312017年01月03日 10:55:18 +00:00Commented Jan 3, 2017 at 10:55 -
4\$\begingroup\$ The old trick: compare against
<1
instead of==0
. \$\endgroup\$manatwork– manatwork2017年01月03日 11:19:24 +00:00Commented Jan 3, 2017 at 11:19 -
\$\begingroup\$ Dang. I reached to
for(;!($argv[1]%$i);$i++);echo$i;
. Yours is the natural evolution of mine. This has my upvote! \$\endgroup\$Ismael Miguel– Ismael Miguel2017年01月05日 19:00:32 +00:00Commented Jan 5, 2017 at 19:00
Cubix, (削除) 14 (削除ここまで) 12 bytes
I2/L/);?%<@O
Saved 2 bytes thanks to MickyT.
Explanation
In cube form, the code is:
I 2
/ L
/ ) ; ? % < @ O
. . . . . . . .
. .
. .
Basically, this just takes the input and starts a counter. It then checks each successive value of the counter until it finds one that isn't a factor of the input.
-
\$\begingroup\$
I2/L/);?%<@O
for a couple of bytes less. Same general process, just different path \$\endgroup\$MickyT– MickyT2018年01月30日 20:28:48 +00:00Commented Jan 30, 2018 at 20:28
Rust, 23 bytes
|n|(1..).find(|x|x%n>0)
A closure that iterates over all positive integers returning Some(n)
where n is the first integer modulo input is greater than zero.