Como você deve saber, o fatorial de um número inteiro positivo n
é o produto de todos os números inteiros positivos iguais ou menores a n
.
Por exemplo :
6! = 6*5*4*3*2*1 = 720
0! = 1
Vamos agora definir uma operação especial com um nome irrelevante como sumFac
:
Dado um número inteiro positivo n
,sumFac(n)
é a soma dos fatoriais dos dígitos.
Por exemplo :
sumFac(132) = 1! + 3! + 2! = 9
Tarefa
Sua missão, independentemente de você optar por aceitá-la ou não, é retornar a sequência (potencialmente infinita) dos aplicativos sumFac
para um número inteiro fornecido na entrada.
Exemplo: 132 -> 132, 9, 362880, 81369, 403927, ...
Mas isso não é tudo! De fato, as aplicações de sumFac
eventualmente criarão um ciclo. Você também deve retornar este ciclo!
Se seu idioma tiver um fatorial incorporado, você poderá usá-lo. Não sou exigente quanto ao tipo de retorno, basta retornar a sequência de aplicativos sumFac e o ciclo em um formato compreensível por um ser humano.
EDIT: Para ajudá-lo a visualizar melhor como deve ser a saída, copiei as Leaky Nun's logo abaixo:
[132, 9, 362880, 81369, 403927, 367953, 368772, 51128, 40444, 97, 367920, 368649, 404670, 5810, 40442, 75, 5160, 842, 40346, 775, 10200, 6, 720, 5043, 151, 122, 5, 120, 4, 24, 26, 722, 5044, 169, 363601, 1454]
Você só precisa parar a sequência quando o ciclo estiver prestes a começar pela segunda vez!
Mas isso é código-golfe então a resposta mais curta em bytes vence!
Entre os melhores
Aqui está um snippet de pilha para gerar uma classificação regular e uma visão geral dos vencedores por idioma.
/* Configuration */
var QUESTION_ID = 117583; // 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 = 68509; // 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>
Respostas:
Gelatina , 6 bytes
Experimente online!
Não vejo outra maneira de torná-lo mais curto do que fazer o que foi dito.
Especificações
132
(como argumento da linha de comandos)[132, 9, 362880, 81369, 403927, 367953, 368772, 51128, 40444, 97, 367920, 368649, 404670, 5810, 40442, 75, 5160, 842, 40346, 775, 10200, 6, 720, 5043, 151, 122, 5, 120, 4, 24, 26, 722, 5044, 169, 363601, 1454]
fonte
Python 2 , 88 bytes
Experimente online!
fonte
05AB1E , 12 bytes
Experimente online!
Explicação
fonte
s
resposta errada e agradável.Braquilog , 17 bytes
Experimente online!
Explicação
fonte
I
significa isso ?ᵃ⁾
.ᵃ³
significa "acumular 3 vezes".ᵃ⁾
significa "acumular quantas vezes o último elemento da entrada", que nesse caso éI
. DesdeI
é uma variável completamente livre, ele vai tentar valores para que a partir0
de+inf
.Wolfram Language,
626056 bytesÉ realmente muito ruim que a Wolfram Language tenha nomes de funções tão abomináveis. *Suspiro*
Explicação:
fonte
Tr
.NestWhileList[...,All]
!JavaScript (ES6),
9189 bytesEconomizou 2 bytes graças a fəˈnɛtɪk
Acontece ser bastante semelhante à outra resposta JS .
Mostrar snippet de código
fonte
ClojureScript,
146109 bytesYikes, that is a monstrosity. Someone please help me golf this...
Thanks
@cliffroot
for shaving off a whopping 37 bytes!This is an anonymous function, to run the function, you have to do this:
TIO doesn't have ClojureScript, so here's a link to a ClojureScript REPL.
Here's a link to a Clojure program which prints the last element in the list from 0 to 1000.
Here's the output for
9999
:I have a strong suspicion that all numbers must eventually settle at
1
or the loop[169 363601 1454]
.Ungolfed code:
Explanation coming soon!
fonte
for
can be(for[a s](apply *(range 1(-(int a)47))))
, can't it?let
#(loop[n[%]](let[f(apply +(for[a(str(last n))](apply *(range 1(-(int a)47)))))](if(some #{f}n)n(recur(conj n f)))))
(- ... 47)
in ClojureScript, justint
will suffice(inc(int a))
should do for ClojureScript and(-(int a)47)
for Clojure.Perl 6, 64 bytes
Try it
Expanded:
Every line above that has
{
starts a new bare block lambda with an implicit parameter of$_
.I used
[*] 2..$_
instead of[*] 1..$_
purely as a micro optimization.fonte
JavaScript, 92 bytes
Thanks @Shaggy for golfing off one byte with includes
Thanks @Neil for golfing off two bytes
Code separated into individual functions 92 bytes
Code on one line 92 bytes
Explanation
Initially call the function with just a single argument, therefore a=[].
If x exists in the array a return a
a.includes(x)?a:...
Otherwise, append x to a and pass the factorial digit sum and a to the function
(a.push(x),f(k(x),a))
Factorial Digit sum performed so that it will not exceed the maximum recursion limit.
List of all possible endpoints: 1, 2, 145, 169, 871, 872, 1454, 40585, 45361, 45362, 363601
Try it online!
fonte
f=(x,a=[])=>a.includes(x)?a:(a.push(x),f(k(x),a))
f(k(x),a,a.push(x))
? Also, I think you can writek=n=>n&&
to save another byte.Haskell,
8067 bytesTry it online! Usage:
([]#) 132
Edit: Saved 13 bytes with typs from Ørjan Johansen!
fonte
n
instead ofs
(same as in ovs's Python answer), thenf=([]#)
. (2) Switch the branches, inlines
, and useelem
.++
for:
also.n:
and changing=g
to=[]
, but it seems to be only a tie.Pyth, 9 bytes
Try it online!
This answer uses
.u
("Cumulative fixed-point. Apply until a result that has occurred before is found. Return all intermediate results.")fonte
Pyth, 30 bytes
Try It Here
fonte
Python 3, 110 bytes
Try it online!
fonte
R, 120 Bytes
fonte
o=scan()
, useel()
instead of[[1]]
, andgamma(n+1)=factorial(n)
which I believe saves a byte, and I thinkas.numeric
is the same asas.double
for integers, which also saves a byte, and you can usetoString
instead ofas.character
.Java 9 JSHell, 213 bytes
Try it online!
Note: This solution relies on the string representation of a number having code points in the range 48-57. Works for ASCII, UTF-8, Latin-1, all ISO-8859-* character sets, most code pages. Does not work for EBCDIC. I don't think anyone will deduct points for that. :)
Ungolfed:
Notes:
fonte
Pyth,
2211 bytesTry it online!
Lots of credit to Leaky Nun's answer, which introduced me to
.u
, and helped save a massive 11 bytes of this program.Explanation:
fonte
.u
. I guess I'll need to take a look through the character reference again to see if there are any other useful functions there.`N
to convert to string instead of+Nk
.N
would be obsolete then, and there comes a 9-byte solution...Axiom, 231 bytes
not golfed functions and some test
fonte
Java 7, 220 bytes
Explanation:
Test code:
Try it here.
Output:
fonte
GolfScript, 44 bytes
Try it online!
The factorial part is from here.
fonte
C, 161 bytes
See it work online.
fonte
TI-BASIC,
85796460 bytesSince this is running on a graphing calculator, there is limited RAM. Try testing it with numbers that loop quickly, like
169
.More Explanation:
If prod(L₁-X
works by subtracting the new element from the old list, then multiplying all the elements of the list together. If the element was already in the list, the product will be0
, a falsey value. Otherwise, the product will be a positive integer, a truthy value.fonte
Husk, 6 bytes
Try it online!
fonte
J,
4031 bytesEdit: 9 bytes saved using the improvements by FrownyFrog. Thanks!
Original code:
In this case I decided to count the bytes for the verb definition, since otherwise it doesn't work in the interpreter.
Explanation:
Try it online!
fonte
([:-.e.~)
->(1-e.~)
[:+/!@"."0@":@{:
is the same length, so there's no improvement with10#.inv
. Just had to drop the()
.Tcl, 143 bytes
Try it online!
fonte