74
\$\begingroup\$

Fibonacci + FizzBuzz = Fibo Nacci!


Your challenge is to create a Fibo Nacci program!

  • A Fibo Nacci program outputs the first 100 Fibonacci numbers (starting from 1).
  • If the Fibonacci number is divisible by both 2 and 3 (i.e. it is divisible by 6), then output FiboNacci instead of the number.
  • Otherwise, if the Fibonacci number is divisible by 2, then output Fibo instead of the number.
  • Otherwise, if the Fibonacci number is divisible by 3, then output Nacci instead of the number.

Rules

  • The program should take no input.
  • The program should output a new line (\n) after every entry.
  • The program should not print anything to STDERR.
  • The program must output the first 100 Fibo Nacci entries (starting from 1).
  • Standard loopholes are not allowed (by default).
  • This is so shortest code in bytes wins!

Here is the expected output:

1
1
Fibo
Nacci
5
Fibo
13
Nacci
Fibo
55
89
FiboNacci
233
377
Fibo
Nacci
1597
Fibo
4181
Nacci
Fibo
17711
28657
FiboNacci
75025
121393
Fibo
Nacci
514229
Fibo
1346269
Nacci
Fibo
5702887
9227465
FiboNacci
24157817
39088169
Fibo
Nacci
165580141
Fibo
433494437
Nacci
Fibo
1836311903
2971215073
FiboNacci
7778742049
12586269025
Fibo
Nacci
53316291173
Fibo
139583862445
Nacci
Fibo
591286729879
956722026041
FiboNacci
2504730781961
4052739537881
Fibo
Nacci
17167680177565
Fibo
44945570212853
Nacci
Fibo
190392490709135
308061521170129
FiboNacci
806515533049393
1304969544928657
Fibo
Nacci
5527939700884757
Fibo
14472334024676221
Nacci
Fibo
61305790721611591
99194853094755497
FiboNacci
259695496911122585
420196140727489673
Fibo
Nacci
1779979416004714189
Fibo
4660046610375530309
Nacci
Fibo
19740274219868223167
31940434634990099905
FiboNacci
83621143489848422977
135301852344706746049
Fibo
Nacci

The Catalogue

The Snack Stack Snippet at the bottom of this post generates the catalogue from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

/* Configuration */

var QUESTION_ID = 63442; // Obtain this from the url
// It will be like http://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 41805; // 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 "http://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 "http://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,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\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,
      });
    else console.log(body);
  });
  
  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;
    lang = jQuery('<a>'+lang+'</a>').text();
    
    languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang, 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_raw.toLowerCase() > b.lang_raw.toLowerCase()) return 1;
    if (a.lang_raw.toLowerCase() < b.lang_raw.toLowerCase()) 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="language-list">
  <h2>Shortest Solution 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>
<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>
<table style="display: none">
  <tbody id="answer-template">
    <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>
<table style="display: none">
  <tbody id="language-template">
    <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>

\$\endgroup\$
  • 6
    \$\begingroup\$ What about languages with highest integer type of only 64 bits? :( Isn't 90 fib. numbers enough? \$\endgroup\$ – Zereges Nov 9 '15 at 18:15
  • 3
    \$\begingroup\$ @Zereges In that matter, I am sorry. :( \$\endgroup\$ – Cows quack Nov 9 '15 at 19:30
  • 28
    \$\begingroup\$ Maybe it should be called "Fizzo Nacci" \$\endgroup\$ – LegionMammal978 Nov 9 '15 at 22:56
  • 4
    \$\begingroup\$ @SztupY Because the output in this question is completely invariant, you don't even need integers at all. Just treat this question as a kolmogorov-complexity question (I even added the tag) and go from there. \$\endgroup\$ – Chris Jester-Young Nov 12 '15 at 17:43
  • 3
    \$\begingroup\$ @ChrisJester-Young it's still an unnecessarry limitation, that might make creative implementators steer clear of this task. And most of the solutions (including the 2nd most upvoted one) are breaking already \$\endgroup\$ – SztupY Nov 12 '15 at 19:24

41 Answers 41

1
\$\begingroup\$

Javascript (ES6), 117 108 bytes

let s=[1,1],i;for(i=1;i<101;s[++i]=s[i-1]+s[i-2]);s.map(n=>console.log((n%2?"":"Fibo")+(n%3?"":"Nacci")||n))

Try it online here.

Sadly this breaks after the 77th number, because Javascript doesn't support numbers that high. I think this is still cool enough to warrant an answer, though.

It generates the entire range of numbers, then iterates with map to print out the appropriate string.

Takes advantage of the fact that console.log prints on a new line. Could also use alert, but that's more annoying.

\$\endgroup\$
1
\$\begingroup\$

Haskell, 150 143 126 118 bytes

main=putStr.unlines.take 100$0!1
x!y=d 3"Nacci"(d 2"Fibo"id)(show y):y!(x+y) where d z a f b|mod y z>0=f b|1>0=f""++a

ungolfed:

    main = 
      putStr . unlines . take 100 $ f 0 1
    f x y 
      = d 3 "Nacci" (d 2 "Fibo" id) (show y) : f y (x+y)
      where 
        d z a g b
          | mod y z > 0 = g b
          | otherwise   = g "" ++ a
\$\endgroup\$
  • \$\begingroup\$ Why ; instead of ;? \$\endgroup\$ – Leif Willerts Nov 18 '15 at 21:47
  • \$\begingroup\$ Oups, missed that one byte @LeifWillerts :). \$\endgroup\$ – HaskellElephant Dec 7 '15 at 11:26
  • \$\begingroup\$ Well, seems like that "inspired" to improve much more :) \$\endgroup\$ – Leif Willerts Dec 7 '15 at 19:36
  • \$\begingroup\$ What does d do? \$\endgroup\$ – Cyoce Aug 24 '16 at 23:43
1
\$\begingroup\$

><>, 78 bytes

01:n0\oo~o'F0ibo'\
aa:+1<}[3rn?/!?%3\!?%2:::::+}:}oa;?=*
ooooo'Nacci'/
00$0o\.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Javascript ES6 112 bytes

var f=(a,b,c)=>{if(!(99<c)){var d=a%2?"":"Fibo",e=a%3?"":"Nacci";console.log(d||e?d+e:n);f(b,a+b,c+1)}};f(1,1,0)

As with other JS answers, fails after 73rd number because of js use of floats

\$\endgroup\$
  • \$\begingroup\$ Why var? You can save 8 bytes omitting it, making globals instead. There is no consensus saying that functions can't create globals. Also, you can omit f=, making an anonymous function, saving another -2 bytes. Final save: -10 bytes Final size: 102 bytes \$\endgroup\$ – Erik the Outgolfer Jun 18 '16 at 21:59
0
\$\begingroup\$

Perl, 146 Bytes

use bigint;$a=$b=1;for$i(0..99){$l=($a%2==0?2:0)+($a%3==0?1:0);print$a if$l==0;print"Fibo"if$l&2;print"Nacci"if$l&1;print"\n";($a,$b)=($b,$a+$b);}

Uncompressed:

#!/usr/bin/perl
use bigint;
$a=$b=1;
for$i(0..99) {
    $l=($a%2==0?2:0)+($a%3==0?1:0);
    print $a if $l == 0;
    print "Fibo" if $l & 2;
    print "Nacci" if $l & 1;
    print "\n";
    ($a,$b)=($b,$a+$b);
}

old version with 156 bytes...

use bigint;$a=$b=1;for$i(0..99){if($a%2==0){print"Fibo";print($a%3==0?"Nacci\n":"\n");}elsif($a%3==0){print"Nacci\n";}else{print"$a\n";}($a,$b)=($b,$a+$b);}

Uncompressed:

#!/usr/bin/perl
use bigint;
$a=$b=1;
for $i (0..99) {
    if($a%2 == 0) {
        print "Fibo";
        print ($a%3 == 0 ? "Nacci\n" : "\n");
    } elsif($a%3 == 0) {
        print "Nacci\n";
    } else {
        print "$a\n";
    }
    ($a,$b)=($b,$a+$b);
}
\$\endgroup\$
0
\$\begingroup\$

C, 121 bytes

Had to go full __int128_t for this to work. printf doesn't know how to handle a 128bit integer, so the last four numeric values will be printed wrong. Still, it was fun to code, so here you go:

__int128_t a,b=1,c;i=100;main(){while(i--){c=b;b%=6;printf(b?b%2?b-3?"%lu\n":"Nacci\n":"Fibo\n":"FiboNacci\n",c);b=c+a;a=c;}}

compiled with gcc-4.8 fibo.c on x86_64.

\$\endgroup\$
  • \$\begingroup\$ while is always longer than or as long as for. \$\endgroup\$ – Jonathan Frech Feb 12 '18 at 21:51
0
\$\begingroup\$

Python 3, 137 bytes

Fun question with lots of new things I learnt in doing it. Python3 isn't a golfing language, but I suppose this isn't too bad

f=lambda n:n if n<2 else f(n-2)+f(n-1)
for n in range(100):t=f(-~n);x="Fibo"*(t%2==0)+"Nacci"*(t%3==0);print(x if t%2==0 or t%3==0 else t)

Hardest part was just to get it to only print the number

First line calculates the nth fib number. Second line joins Fibo and Nacci if it fits the rules an then it prints that string if it should else print the nth fib number.

\$\endgroup\$
  • 1
    \$\begingroup\$ 2else instead of 2 else for -1 byte, n+1 instead of -~n because -~n is unneeded, t%2<1 instead of t%2==0 for -1 byte, t%3<1 instead of t%3==0 for -1 byte, t%2<1or t%3<1else instead of t%2==0 or t%3==0 else for -4 bytes. Total save: -7 bytes. \$\endgroup\$ – Erik the Outgolfer Jun 18 '16 at 21:48
0
\$\begingroup\$

///, 642 bytes

/A/
Fibo//B/
Nacci//*/ANacci
//^/AB
/1
1^5A
13BA
55
89*233
377^1597A
4181BA
17711
28657*75025
121393^514229A
1346269BA
5702887
9227465*24157817
39088169^165580141A
433494437BA
1836311903
2971215073*7778742049
12586269025^53316291173A
139583862445BA
591286729879
956722026041*2504730781961
4052739537881^17167680177565A
44945570212853BA
190392490709135
308061521170129*806515533049393
1304969544928657^5527939700884757A
14472334024676221BA
61305790721611591
99194853094755497*259695496911122585
420196140727489673^1779979416004714189A
4660046610375530309BA
19740274219868223167
31940434634990099905*83621143489848422977
135301852344706746049AB
\$\endgroup\$
0
\$\begingroup\$

F#, 153 134 bytes

let rec L c n p=printfn"%s"(c%6m|>function|0m->"FiboNacci"|2m|4m->"Fibo"|3m->"Nacci"|_->string c);if n<100 then L(p+c)(n+1)c
L 1I 1 0I

Edit: Used decimal instead of bigint which allowed using pattern matching. Stole the internal logic from the other F# answer. My older filtering logic is in the below snippets.

One liner, same logic, 155 bytes:

let rec L c n p=printfn"%s"(if c%6I=0I then"FiboNacci"elif c%2I=0I then"Fibo"elif c%3I=0I then"Nacci"else string c);if n<100 then L(p+c)(n+1)c in L 1I 1 0I

Different logic, 168 bytes:

Seq.unfold(fun(p,c)->Some((if c%6I=0I then"FiboNacci"elif c%2I=0I then"Fibo"elif c%3I=0I then"Nacci"else string c),(c,c+p)))(0I,1I)|>Seq.take 100|>Seq.iter(printfn"%s")
\$\endgroup\$
0
\$\begingroup\$

Perl6, 59 58

say "Fibo"x$_%%2~"Nacci"x$_%%3||$_ for (1,1,*+*...*)[^100]
\$\endgroup\$
0
\$\begingroup\$

Javascript, 157 155 bytes

Golfed:

function g(n){a=!n%2;b=!n%3;a&&b?return "FiboNacci":;b?return "Nacci";a?return "Fibo";return n==1||n==2?1:f(n-1)+f(n-2)}for(n=100;n++>0;)console.log(f(n))

Ungolfed:

function fibonacci(n){
  if(n==1||n==2) return 1;
  return(fibonacci(n-1)+fibonacci(n-2));
}
function fibonacci2(n){
  a=!(n%2);
  b=!(n%3);
  if(a&&b) return "FiboNacci";
  if(a) return "Fibo";
  if(b) return "Nacci";
  return fibonacci(n);
}
for(n=100;n++>0;) console.log(fibonacci2(n));

Saved 3 bytes thanks to Cows Quack's comment

\$\endgroup\$
  • \$\begingroup\$ Thanks! By the way, why is there a minimum comment length? \$\endgroup\$ – Solomon Ucko Nov 12 '15 at 21:02
  • \$\begingroup\$ There is a minimum comment length to prevent comments like "Good Job!" or "+1" and stuff like that. \$\endgroup\$ – Cows quack Nov 13 '15 at 6:26
  • \$\begingroup\$ @ΚριτικσιΛίθος Why is there also a minimum amount of reputation to be able to post comments then? \$\endgroup\$ – Solomon Ucko Nov 15 '15 at 23:32
  • \$\begingroup\$ Change your for-loop to for(n=100;n++>0;)console.log(f(n)) It saves 3 bytes \$\endgroup\$ – Cows quack Nov 16 '15 at 14:24
  • \$\begingroup\$ @ΚριτικσιΛίθος OK, thanks. Editing post. \$\endgroup\$ – Solomon Ucko Nov 16 '15 at 22:16

Your Answer

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

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