Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,3 +1,8 @@
Write a program that prints the integers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". [http://weblog.raganwald.com/2007/01/dont-overthink-fizzbuzz.html]
Write a program that prints the integers from 1 to 100.
FizzBuzz was presented as the lowest level of comprehension required to illustrate adequacy. [http://www.codinghorror.com/blog/archives/000804.html]
But for multiples of three print "Fizz" instead of the number,
and for the multiples of five print "Buzz". <br>
For numbers which are multiples of both three and five print "FizzBuzz". [http://weblog.raganwald.com/2007/01/dont-overthink-fizzbuzz.html]
FizzBuzz was presented as the lowest level of comprehension
required to illustrate adequacy. [http://blog.codinghorror.com/fizzbuzz-the-programmers-stairway-to-heaven/]

View file

@ -2,4 +2,5 @@
category:
- Iteration
- Recursion
- Simple
note: Classic CS problems and programs

View file

@ -0,0 +1 @@
A[I]1+I(0A)/A('FIZZBUZZ' 'FIZZ 'BUZZ' 0)[2¨×(3 5)|¨1+100]

View file

@ -1,18 +1,18 @@
#include <iostream>
using namespace std;
int main () {
int i;
for (i = 0; i <= 100; i++) {
int main ()
{
for (int i = 1; i <= 100; i++)
{
if ((i % 15) == 0)
cout << "FizzBuzz" << endl;
cout << "FizzBuzz\n";
else if ((i % 3) == 0)
cout << "Fizz" << endl;
cout << "Fizz\n";
else if ((i % 5) == 0)
cout << "Buzz" << endl;
cout << "Buzz\n";
else
cout << i << endl;
cout << i << "\n";
}
return 0;
}

View file

@ -13,7 +13,7 @@ int main()
cout << "Buzz";
if (!fizz && !buzz)
cout << i;
cout << endl;
cout << "\n";
}
return 0;
}

View file

@ -1,25 +1,16 @@
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> range(100);
std::iota(range.begin(), range.end(), 1);
int i, f = 2, b = 4;
std::vector<std::string> values;
values.resize(range.size());
for ( i = 1 ; i <= 100 ; ++i, --f, --b )
{
if ( f && b ) { std::cout << i; }
if ( !f ) { std::cout << "Fizz"; f = 3; }
if ( !b ) { std::cout << "Buzz"; b = 5; }
std::cout << std::endl;
}
auto fizzbuzz = [](int i) -> std::string {
if ((i%15) == 0) return "FizzBuzz";
if ((i%5) == 0) return "Buzz";
if ((i%3) == 0) return "Fizz";
return std::to_string(i);
};
std::transform(range.begin(), range.end(), values.begin(), fizzbuzz);
for (auto& str: values) std::cout << str << std::endl;
return 0;
return 0;
}

View file

@ -1,48 +1,25 @@
#include <iostream>
template <int n, int m3, int m5>
struct fizzbuzz : fizzbuzz<n-1, (n-1)%3, (n-1)%5>
{
fizzbuzz()
{ std::cout << n << std::endl; }
};
template <int n>
struct fizzbuzz<n, 0, 0> : fizzbuzz<n-1, (n-1)%3, (n-1)%5>
{
fizzbuzz()
{ std::cout << "FizzBuzz" << std::endl; }
};
template <int n, int p>
struct fizzbuzz<n, 0, p> : fizzbuzz<n-1, (n-1)%3, (n-1)%5>
{
fizzbuzz()
{ std::cout << "Fizz" << std::endl; }
};
template <int n, int p>
struct fizzbuzz<n, p, 0> : fizzbuzz<n-1, (n-1)%3, (n-1)%5>
{
fizzbuzz()
{ std::cout << "Buzz" << std::endl; }
};
template <>
struct fizzbuzz<0,0,0>
{
fizzbuzz()
{ std::cout << 0 << std::endl; }
};
template <int n>
struct fb_run
{
fizzbuzz<n, n%3, n%5> fb;
};
#include <algorithm>
#include <vector>
int main()
{
fb_run<100> fb;
std::vector<int> range(100);
std::iota(range.begin(), range.end(), 1);
std::vector<std::string> values;
values.resize(range.size());
auto fizzbuzz = [](int i) -> std::string {
if ((i%15) == 0) return "FizzBuzz";
if ((i%5) == 0) return "Buzz";
if ((i%3) == 0) return "Fizz";
return std::to_string(i);
};
std::transform(range.begin(), range.end(), values.begin(), fizzbuzz);
for (auto& str: values) std::cout << str << std::endl;
return 0;
}

View file

@ -1,164 +1,48 @@
#include <iostream>
#include <string>
#include <cstdlib>
#include <boost/mpl/string.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/size_t.hpp>
using namespace std;
using namespace boost;
///////////////////////////////////////////////////////////////////////////////
// exponentiation calculations
template <int accum, int base, int exp> struct POWER_CORE : POWER_CORE<accum * base, base, exp - 1>{};
template <int accum, int base>
struct POWER_CORE<accum, base, 0>
template <int n, int m3, int m5>
struct fizzbuzz : fizzbuzz<n-1, (n-1)%3, (n-1)%5>
{
enum : int { val = accum };
fizzbuzz()
{ std::cout << n << std::endl; }
};
template <int base, int exp> struct POWER : POWER_CORE<1, base, exp>{};
///////////////////////////////////////////////////////////////////////////////
// # of digit calculations
template <int depth, unsigned int i> struct NUM_DIGITS_CORE : NUM_DIGITS_CORE<depth + 1, i / 10>{};
template <int depth>
struct NUM_DIGITS_CORE<depth, 0>
template <int n>
struct fizzbuzz<n, 0, 0> : fizzbuzz<n-1, (n-1)%3, (n-1)%5>
{
enum : int { val = depth};
fizzbuzz()
{ std::cout << "FizzBuzz" << std::endl; }
};
template <int i> struct NUM_DIGITS : NUM_DIGITS_CORE<0, i>{};
template <>
struct NUM_DIGITS<0>
template <int n, int p>
struct fizzbuzz<n, 0, p> : fizzbuzz<n-1, (n-1)%3, (n-1)%5>
{
enum : int { val = 1 };
fizzbuzz()
{ std::cout << "Fizz" << std::endl; }
};
///////////////////////////////////////////////////////////////////////////////
// Convert digit to character (1 -> '1')
template <int i>
struct DIGIT_TO_CHAR
template <int n, int p>
struct fizzbuzz<n, p, 0> : fizzbuzz<n-1, (n-1)%3, (n-1)%5>
{
enum : char{ val = i + 48 };
};
///////////////////////////////////////////////////////////////////////////////
// Find the digit at a given offset into a number of the form 0000000017
template <unsigned int i, int place> // place -> [0 .. 10]
struct DIGIT_AT
{
enum : char{ val = (i / POWER<10, place>::val) % 10 };
};
struct NULL_CHAR
{
enum : char{ val = '\0' };
};
///////////////////////////////////////////////////////////////////////////////
// Convert the digit at a given offset into a number of the form '0000000017' to a character
template <unsigned int i, int place> // place -> [0 .. 9]
struct ALT_CHAR : DIGIT_TO_CHAR< DIGIT_AT<i, place>::val >{};
///////////////////////////////////////////////////////////////////////////////
// Convert the digit at a given offset into a number of the form '17' to a character
// Template description, with specialization to generate null characters for out of range offsets
template <unsigned int i, int offset, int numDigits, bool inRange>
struct OFFSET_CHAR_CORE_CHECKED{};
template <unsigned int i, int offset, int numDigits>
struct OFFSET_CHAR_CORE_CHECKED<i, offset, numDigits, false> : NULL_CHAR{};
template <unsigned int i, int offset, int numDigits>
struct OFFSET_CHAR_CORE_CHECKED<i, offset, numDigits, true> : ALT_CHAR<i, (numDigits - offset) - 1 >{};
// Perform the range check and pass it on
template <unsigned int i, int offset, int numDigits>
struct OFFSET_CHAR_CORE : OFFSET_CHAR_CORE_CHECKED<i, offset, numDigits, offset < numDigits>{};
// Calc the number of digits and pass it on
template <unsigned int i, int offset>
struct OFFSET_CHAR : OFFSET_CHAR_CORE<i, offset, NUM_DIGITS<i>::val>{};
///////////////////////////////////////////////////////////////////////////////
// Integer to char* template. Works on unsigned ints.
template <unsigned int i>
struct IntToStr
{
const static char str[];
typedef typename mpl::string<
OFFSET_CHAR<i, 0>::val,
OFFSET_CHAR<i, 1>::val,
OFFSET_CHAR<i, 2>::val,
OFFSET_CHAR<i, 3>::val,
OFFSET_CHAR<i, 4>::val,
OFFSET_CHAR<i, 5>::val,
/*OFFSET_CHAR<i, 6>::val,
OFFSET_CHAR<i, 7>::val,
OFFSET_CHAR<i, 8>::val,
OFFSET_CHAR<i, 9>::val,*/
NULL_CHAR::val>::type type;
};
template <unsigned int i>
const char IntToStr<i>::str[] =
{
OFFSET_CHAR<i, 0>::val,
OFFSET_CHAR<i, 1>::val,
OFFSET_CHAR<i, 2>::val,
OFFSET_CHAR<i, 3>::val,
OFFSET_CHAR<i, 4>::val,
OFFSET_CHAR<i, 5>::val,
OFFSET_CHAR<i, 6>::val,
OFFSET_CHAR<i, 7>::val,
OFFSET_CHAR<i, 8>::val,
OFFSET_CHAR<i, 9>::val,
NULL_CHAR::val
};
template <bool condition, class Then, class Else>
struct IF
{
typedef Then RET;
};
template <class Then, class Else>
struct IF<false, Then, Else>
{
typedef Else RET;
};
template < typename Str1, typename Str2 >
struct concat : mpl::insert_range<Str1, typename mpl::end<Str1>::type, Str2> {};
template <typename Str1, typename Str2, typename Str3 >
struct concat3 : mpl::insert_range<Str1, typename mpl::end<Str1>::type, typename concat<Str2, Str3 >::type > {};
typedef typename mpl::string<'f','i','z','z'>::type fizz;
typedef typename mpl::string<'b','u','z','z'>::type buzz;
typedef typename mpl::string<'\r', '\n'>::type mpendl;
typedef typename concat<fizz, buzz>::type fizzbuzz;
// discovered boost mpl limitation on some length
template <int N>
struct FizzBuzz
{
typedef typename concat3<typename FizzBuzz<N - 1>::type, typename IF<N % 15 == 0, typename fizzbuzz::type, typename IF<N % 3 == 0, typename fizz::type, typename IF<N % 5 == 0, typename buzz::type, typename IntToStr<N>::type >::RET >::RET >::RET, typename mpendl::type>::type type;
fizzbuzz()
{ std::cout << "Buzz" << std::endl; }
};
template <>
struct FizzBuzz<1>
struct fizzbuzz<0,0,0>
{
typedef mpl::string<'1','\r','\n'>::type type;
fizzbuzz()
{ std::cout << 0 << std::endl; }
};
int main(int argc, char** argv)
template <int n>
struct fb_run
{
const int n = 7;
std::cout << mpl::c_str<FizzBuzz<n>::type>::value << std::endl;
return 0;
fizzbuzz<n, n%3, n%5> fb;
};
int main()
{
fb_run<100> fb;
return 0;
}

View file

@ -0,0 +1,164 @@
#include <iostream>
#include <string>
#include <cstdlib>
#include <boost/mpl/string.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/size_t.hpp>
using namespace std;
using namespace boost;
///////////////////////////////////////////////////////////////////////////////
// exponentiation calculations
template <int accum, int base, int exp> struct POWER_CORE : POWER_CORE<accum * base, base, exp - 1>{};
template <int accum, int base>
struct POWER_CORE<accum, base, 0>
{
enum : int { val = accum };
};
template <int base, int exp> struct POWER : POWER_CORE<1, base, exp>{};
///////////////////////////////////////////////////////////////////////////////
// # of digit calculations
template <int depth, unsigned int i> struct NUM_DIGITS_CORE : NUM_DIGITS_CORE<depth + 1, i / 10>{};
template <int depth>
struct NUM_DIGITS_CORE<depth, 0>
{
enum : int { val = depth};
};
template <int i> struct NUM_DIGITS : NUM_DIGITS_CORE<0, i>{};
template <>
struct NUM_DIGITS<0>
{
enum : int { val = 1 };
};
///////////////////////////////////////////////////////////////////////////////
// Convert digit to character (1 -> '1')
template <int i>
struct DIGIT_TO_CHAR
{
enum : char{ val = i + 48 };
};
///////////////////////////////////////////////////////////////////////////////
// Find the digit at a given offset into a number of the form 0000000017
template <unsigned int i, int place> // place -> [0 .. 10]
struct DIGIT_AT
{
enum : char{ val = (i / POWER<10, place>::val) % 10 };
};
struct NULL_CHAR
{
enum : char{ val = '\0' };
};
///////////////////////////////////////////////////////////////////////////////
// Convert the digit at a given offset into a number of the form '0000000017' to a character
template <unsigned int i, int place> // place -> [0 .. 9]
struct ALT_CHAR : DIGIT_TO_CHAR< DIGIT_AT<i, place>::val >{};
///////////////////////////////////////////////////////////////////////////////
// Convert the digit at a given offset into a number of the form '17' to a character
// Template description, with specialization to generate null characters for out of range offsets
template <unsigned int i, int offset, int numDigits, bool inRange>
struct OFFSET_CHAR_CORE_CHECKED{};
template <unsigned int i, int offset, int numDigits>
struct OFFSET_CHAR_CORE_CHECKED<i, offset, numDigits, false> : NULL_CHAR{};
template <unsigned int i, int offset, int numDigits>
struct OFFSET_CHAR_CORE_CHECKED<i, offset, numDigits, true> : ALT_CHAR<i, (numDigits - offset) - 1 >{};
// Perform the range check and pass it on
template <unsigned int i, int offset, int numDigits>
struct OFFSET_CHAR_CORE : OFFSET_CHAR_CORE_CHECKED<i, offset, numDigits, offset < numDigits>{};
// Calc the number of digits and pass it on
template <unsigned int i, int offset>
struct OFFSET_CHAR : OFFSET_CHAR_CORE<i, offset, NUM_DIGITS<i>::val>{};
///////////////////////////////////////////////////////////////////////////////
// Integer to char* template. Works on unsigned ints.
template <unsigned int i>
struct IntToStr
{
const static char str[];
typedef typename mpl::string<
OFFSET_CHAR<i, 0>::val,
OFFSET_CHAR<i, 1>::val,
OFFSET_CHAR<i, 2>::val,
OFFSET_CHAR<i, 3>::val,
OFFSET_CHAR<i, 4>::val,
OFFSET_CHAR<i, 5>::val,
/*OFFSET_CHAR<i, 6>::val,
OFFSET_CHAR<i, 7>::val,
OFFSET_CHAR<i, 8>::val,
OFFSET_CHAR<i, 9>::val,*/
NULL_CHAR::val>::type type;
};
template <unsigned int i>
const char IntToStr<i>::str[] =
{
OFFSET_CHAR<i, 0>::val,
OFFSET_CHAR<i, 1>::val,
OFFSET_CHAR<i, 2>::val,
OFFSET_CHAR<i, 3>::val,
OFFSET_CHAR<i, 4>::val,
OFFSET_CHAR<i, 5>::val,
OFFSET_CHAR<i, 6>::val,
OFFSET_CHAR<i, 7>::val,
OFFSET_CHAR<i, 8>::val,
OFFSET_CHAR<i, 9>::val,
NULL_CHAR::val
};
template <bool condition, class Then, class Else>
struct IF
{
typedef Then RET;
};
template <class Then, class Else>
struct IF<false, Then, Else>
{
typedef Else RET;
};
template < typename Str1, typename Str2 >
struct concat : mpl::insert_range<Str1, typename mpl::end<Str1>::type, Str2> {};
template <typename Str1, typename Str2, typename Str3 >
struct concat3 : mpl::insert_range<Str1, typename mpl::end<Str1>::type, typename concat<Str2, Str3 >::type > {};
typedef typename mpl::string<'f','i','z','z'>::type fizz;
typedef typename mpl::string<'b','u','z','z'>::type buzz;
typedef typename mpl::string<'\r', '\n'>::type mpendl;
typedef typename concat<fizz, buzz>::type fizzbuzz;
// discovered boost mpl limitation on some length
template <int N>
struct FizzBuzz
{
typedef typename concat3<typename FizzBuzz<N - 1>::type, typename IF<N % 15 == 0, typename fizzbuzz::type, typename IF<N % 3 == 0, typename fizz::type, typename IF<N % 5 == 0, typename buzz::type, typename IntToStr<N>::type >::RET >::RET >::RET, typename mpendl::type>::type type;
};
template <>
struct FizzBuzz<1>
{
typedef mpl::string<'1','\r','\n'>::type type;
};
int main(int argc, char** argv)
{
const int n = 7;
std::cout << mpl::c_str<FizzBuzz<n>::type>::value << std::endl;
return 0;
}

View file

@ -1,20 +1,5 @@
#include<stdio.h>
#include <stdio.h>
int main (void)
{
int i;
for (i = 1; i <= 100; i++)
{
if (!(i % 15))
printf ("FizzBuzz");
else if (!(i % 3))
printf ("Fizz");
else if (!(i % 5))
printf ("Buzz");
else
printf ("%d", i);
printf("\n");
}
return 0;
int main() {
for (int i=1; i<=105; i++) if (i%3 && i%5) printf("%3d ", i); else printf("%s%s%s", i%3?"":"Fizz", i%5?"":"Buzz", i%15?" ":"\n");
}

View file

@ -1,16 +1,20 @@
#include <stdio.h>
#include<stdio.h>
main() {
int i = 1;
while(i <= 100) {
if(i % 15 == 0)
puts("FizzBuzz");
else if(i % 3 == 0)
puts("Fizz");
else if(i % 5 == 0)
puts("Buzz");
else
printf("%d\n", i);
i++;
}
int main (void)
{
int i;
for (i = 1; i <= 100; i++)
{
if (!(i % 15))
printf ("FizzBuzz");
else if (!(i % 3))
printf ("Fizz");
else if (!(i % 5))
printf ("Buzz");
else
printf ("%d", i);
printf("\n");
}
return 0;
}

View file

@ -1,3 +1,16 @@
#include <stdio.h>
#define F(x,y) printf("%s",i%x?"":#y"zz")
int main(int i){for(--i;i++^100;puts(""))F(3,Fi)|F(5,Bu)||printf("%i",i);return 0;}
main() {
int i = 1;
while(i <= 100) {
if(i % 15 == 0)
puts("FizzBuzz");
else if(i % 3 == 0)
puts("Fizz");
else if(i % 5 == 0)
puts("Buzz");
else
printf("%d\n", i);
i++;
}
}

View file

@ -1,11 +1,3 @@
#include<stdio.h>
int main ()
{
int i;
const char *s[] = { "%d\n", "Fizz\n", s[3] + 4, "FizzBuzz\n" };
for (i = 1; i <= 100; i++)
printf(s[!(i % 3) + 2 * !(i % 5)], i);
return 0;
}
#include <stdio.h>
#define F(x,y) printf("%s",i%x?"":#y"zz")
int main(int i){for(--i;i++^100;puts(""))F(3,Fi)|F(5,Bu)||printf("%i",i);return 0;}

View file

@ -0,0 +1,11 @@
#include<stdio.h>
int main ()
{
int i;
const char *s[] = { "%d\n", "Fizz\n", s[3] + 4, "FizzBuzz\n" };
for (i = 1; i <= 100; i++)
printf(s[!(i % 3) + 2 * !(i % 5)], i);
return 0;
}

View file

@ -0,0 +1,11 @@
#include <stdio.h>
int main(void)
{
for (int i = 1; i <= 100; ++i) {
if (i % 3 == 0) printf("fizz");
if (i % 5 == 0) printf("buzz");
if (i * i * i * i % 15 == 1) printf("%d", i);
puts("");
}
}

View file

@ -1,9 +1,12 @@
Procedure Main()
Local n
Local cFB
For n := 1 to 100
PROCEDURE Main()
LOCAL n
LOCAL cFB
FOR n := 1 TO 100
cFB := ""
AEval( {{3,"Fizz"},{5,"Buzz"}}, {|x| cFB += Iif((n % x[1])==0, x[2], "")})
?? Iif(cFB == "", LTrim(Str(n)), cFB) + Iif(n == 100, ".", ", ")
Next
Return
AEval( { { 3, "Fizz" }, { 5, "Buzz" } }, {|x| cFB += iif( ( n % x[ 1 ] ) == 0, x[ 2 ], "" ) } )
?? iif( cFB == "", LTrim( Str( n ) ), cFB ) + iif( n == 100, ".", ", " )
NEXT
RETURN

View file

@ -1,5 +1,9 @@
(map (fn [x] (cond (zero? (mod x 15)) "FizzBuzz"
(zero? (mod x 5)) "Buzz"
(zero? (mod x 3)) "Fizz"
:else x))
(range 1 101))
(defn fizzbuzz [start finish] (map (fn [n]
(cond
(zero? (mod n 3)) "Fizz"
(zero? (mod n 5)) "Buzz"
(zero? (mod n 15)) "FizzBuzz"
:else n))
(range start finish))
)
(fizzbuzz 1 100)

View file

@ -1 +1,5 @@
(map #(let [s (str (if (zero? (mod % 3)) "Fizz") (if (zero? (mod % 5)) "Buzz"))] (if (empty? s) % s)) (range 1 101))
(map (fn [x] (cond (zero? (mod x 15)) "FizzBuzz"
(zero? (mod x 5)) "Buzz"
(zero? (mod x 3)) "Fizz"
:else x))
(range 1 101))

View file

@ -1,6 +1 @@
(def fizzbuzz (map
#(cond (zero? (mod % 15)) "FizzBuzz"
(zero? (mod % 5)) "Buzz"
(zero? (mod % 3)) "Fizz"
:else %)
(iterate inc 1)))
(map #(let [s (str (if (zero? (mod % 3)) "Fizz") (if (zero? (mod % 5)) "Buzz"))] (if (empty? s) % s)) (range 1 101))

View file

@ -1,13 +1,6 @@
(defn fizz-buzz
([] (fizz-buzz (range 1 101)))
([lst]
(letfn [(fizz? [n] (zero? (mod n 3)))
(buzz? [n] (zero? (mod n 5)))]
(let [f "Fizz"
b "Buzz"
items (map (fn [n]
(cond (and (fizz? n) (buzz? n)) (str f b)
(fizz? n) f
(buzz? n) b
:else n))
lst)] items))))
(def fizzbuzz (map
#(cond (zero? (mod % 15)) "FizzBuzz"
(zero? (mod % 5)) "Buzz"
(zero? (mod % 3)) "Fizz"
:else %)
(iterate inc 1)))

View file

@ -1,6 +1,13 @@
(map (fn [n]
(if-let [fb (seq (concat (when (zero? (mod n 3)) "Fizz")
(when (zero? (mod n 5)) "Buzz")))]
(apply str fb)
n))
(range 1 101))
(defn fizz-buzz
([] (fizz-buzz (range 1 101)))
([lst]
(letfn [(fizz? [n] (zero? (mod n 3)))
(buzz? [n] (zero? (mod n 5)))]
(let [f "Fizz"
b "Buzz"
items (map (fn [n]
(cond (and (fizz? n) (buzz? n)) (str f b)
(fizz? n) f
(buzz? n) b
:else n))
lst)] items))))

View file

@ -1,4 +1,6 @@
(take 100 (map #(let [s (str %2 %3) ] (if (seq s) s (inc %)) )
(range)
(cycle [ "" "" "Fizz" ])
(cycle [ "" "" "" "" "Buzz" ])))
(map (fn [n]
(if-let [fb (seq (concat (when (zero? (mod n 3)) "Fizz")
(when (zero? (mod n 5)) "Buzz")))]
(apply str fb)
n))
(range 1 101))

View file

@ -0,0 +1,4 @@
(take 100 (map #(let [s (str %2 %3) ] (if (seq s) s (inc %)) )
(range)
(cycle [ "" "" "Fizz" ])
(cycle [ "" "" "" "" "Buzz" ])))

View file

@ -0,0 +1 @@
(map #(nth (conj (cycle [% % "Fizz" % "Buzz" "Fizz" % % % "Buzz" % "Fizz" % % "FizzBuzz"]) %) %) (range 1 101))

View file

@ -0,0 +1,7 @@
<Cfloop from="1" to="100" index="i">
<Cfif i mod 15 eq 0>FizzBuzz
<Cfelseif i mod 5 eq 0>Fizz
<Cfelseif i mod 3 eq 0>Buzz
<Cfelse><Cfoutput>#i#</Cfoutput>
</Cfif>
</Cfloop>

View file

@ -1,35 +1,53 @@
import std.stdio: writeln;
import std.stdio, std.algorithm, std.conv;
// with if-else
void fizzBuzz(int n) {
foreach (i; 1 .. n+1)
/// With if-else.
void fizzBuzz(in uint n) {
foreach (immutable i; 1 .. n + 1)
if (!(i % 15))
writeln("FizzBuzz");
"FizzBuzz".writeln;
else if (!(i % 3))
writeln("Fizz");
"Fizz".writeln;
else if (!(i % 5))
writeln("Buzz");
"Buzz".writeln;
else
writeln(i);
i.writeln;
}
// with switch case
void fizzBuzzSwitch(int n) {
foreach (i; 1 .. n+1)
switch(i % 15) {
/// With switch case.
void fizzBuzzSwitch(in uint n) {
foreach (immutable i; 1 .. n + 1)
switch (i % 15) {
case 0:
writeln("FizzBuzz"); break;
"FizzBuzz".writeln;
break;
case 3, 6, 9, 12:
writeln("Fizz"); break;
"Fizz".writeln;
break;
case 5, 10:
writeln("Buzz"); break;
"Buzz".writeln;
break;
default:
writeln(i);
i.writeln;
}
}
void main() {
fizzBuzz(100);
writeln();
fizzBuzzSwitch(100);
void fizzBuzzSwitch2(in uint n) {
foreach (immutable i; 1 .. n + 1)
(i % 15).predSwitch(
0, "FizzBuzz",
3, "Fizz",
5, "Buzz",
6, "Fizz",
9, "Fizz",
10, "Buzz",
12, "Fizz",
/*else*/ i.text).writeln;
}
void main() {
100.fizzBuzz;
writeln;
100.fizzBuzzSwitch;
writeln;
100.fizzBuzzSwitch2;
}

View file

@ -0,0 +1,35 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
do
fizzbuzz
end
fizzbuzz
local
i: INTEGER
do
from
i:= 1
until
i>100
loop
if i\\15= 0 then
io.put_string ("FIZZBUZZ%N")
elseif i\\3=0 then
io.put_string ("FIZZ%N")
elseif i\\5=0 then
io.put_string ("BUZZ%N")
else
io.put_string (i.out + "%N")
end
i:= i+1
end
end
end

View file

@ -0,0 +1,13 @@
#!/usr/bin/env elixir
1..100 |> Enum.map(fn i ->
cond do
rem(i,3*5) == 0 ->
"fizzbuzz"
rem(i,3) == 0 ->
"fizz"
rem(i,5) == 0 ->
"buzz"
true ->
i
end
end) |> Enum.map(fn i -> IO.puts i end)

View file

@ -1,21 +1,7 @@
for (var i = 1; i <= 100; i++) {
if (i % 15 == 0) {
console.log("FizzBuzz");
} else if (i % 3 == 0) {
console.log("Fizz");
} else if (i % 5 == 0) {
console.log("Buzz");
} else {
console.log(i);
}
var i, output;
for (i = 1; i < 101; i++) {
output = '';
if (!(i % 3)) output += 'Fizz';
if (!(i % 5)) output += 'Buzz';
console.log(output || i);
}
Array.apply(null, { length: 100 }).map(function(n, i) {
++i;
return !(i % 15) ?
"FizzBuzz" :
!(i % 3) ?
"Fizz" :
!(i % 5) ?
"Buzz" : i;
}).join("\r\n");

View file

@ -1 +1,25 @@
for (var i=1; i<=100; i++) console.log( (i % 3 === 0 ? 'Fizz' : '') + (i % 5 === 0 ? 'Buzz' : '') || i );
var divs = [15, 3, 5];
var says = ['FizzBuzz', 'Fizz', 'Buzz'];
function fizzBuzz(first, last) {
for (var n = first; n <= last; n++) {
print(getFizzBuzz(n));
}
}
function getFizzBuzz(n) {
var sayWhat = n;
for (var d = 0; d < divs.length; d++) {
if (isMultOf(n, divs[d])) {
sayWhat = says[d];
break;
}
}
return sayWhat;
}
function isMultOf(n, d) {
return n % d == 0;
}
fizzBuzz(1, 100);

View file

@ -1,4 +1,4 @@
fb(i::Int) = "Fizz" ^ (i%3==0) *
"Buzz" ^ (i%5==0) *
string(i) ^ (i%3!=0 && i%5!=0)
[println(fb(i)) for i = 1:100];
for i=1:100 println(fb(i)) end

View file

@ -0,0 +1 @@
map((x) -> x % 15 == 0 ? "FizzBuzz" : (x % 5 == 0 ? "Buzz" : (x % 3 == 0 ? "Fizz" : x)), 1:100)

View file

@ -0,0 +1,7 @@
fib(i) = let result = ""
(i%3 == 0 && (result *="Fizz" ; true)) |
(i%5 == 0 && (result *="Buzz" ; true)) ||
(result = string(i))
result
end
println([fib(i) for i = 1:100])

View file

@ -0,0 +1,4 @@
for i = 1:100
msg = "Fizz" ^ (i%3==0) * "Buzz" ^ (i%5==0)
println(msg=="" ? i : msg)
end

View file

@ -7,7 +7,7 @@ for i = 1, 100 do
output = output.."Buzz"
end
if(output == "") then
output = output..i
output = i
end
print(output)
end

View file

@ -0,0 +1 @@
seq(print(`if`(modp(n,3)=0,`if`(modp(n,15)=0,"FizzBuzz","Fizz"),`if`(modp(n,5)=0,"Buzz",n))),n=1..100):

View file

@ -0,0 +1,2 @@
fizzbuzz1 := n->`if`(modp(n,3)=0,`if`(modp(n,15)=0,"FizzBuzz","Fizz"),`if`(modp(n,5)=0,"Buzz",n)):
for i to 100 do fizzbuzz1(i); od;

View file

@ -0,0 +1,2 @@
fizzbuzz2 := n->piecewise(modp(n,15)=0,"FizzBuzz",modp(n,3)=0,"Fizz",modp(n,5)=0,"Buzz",n):
for i to 100 do fizzbuzz2(i); od;

View file

@ -0,0 +1,8 @@
fizzbuzz3 := proc(n) local r;
r:=map2(modp,n,[3,5]);
if r=[0,0] then "FizzBuzz"
elif r[1]=0 then "Fizz"
elif r[2]=0 then "Buzz"
else n fi;
end proc:
for i to 100 do fizzbuzz3(i); od;

View file

@ -1,26 +1,32 @@
:- module fizzbuzz.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int, string, bool.
:- func fizz(int) = bool.
:- func buzz(int) = bool.
fizz(N) = ( if N mod 3 = 0 then yes else no ).
:- func buzz(int) = bool.
buzz(N) = ( if N mod 5 = 0 then yes else no ).
:- pred fizzbuzz(int::in, bool::in, bool::in, string::out) is det.
% 3? 5?
fizzbuzz(_, yes, yes, "FizzBuzz").
fizzbuzz(_, yes, no, "Fizz").
fizzbuzz(_, no, yes, "Buzz").
fizzbuzz(N, no, no, S) :- S = from_int(N).
% N 3? 5?
:- func fizzbuzz(int, bool, bool) = string.
fizzbuzz(_, yes, yes) = "FizzBuzz".
fizzbuzz(_, yes, no) = "Fizz".
fizzbuzz(_, no, yes) = "Buzz".
fizzbuzz(N, no, no) = from_int(N).
main(!IO) :- main(1, 100, !IO).
:- pred main(int::in, int::in, io::di, io::uo) is det.
main(N, To, !IO) :-
io.write_string(S, !IO), io.nl(!IO),
fizzbuzz(N, fizz(N), buzz(N), S),
( if N < To then main(N + 1, To, !IO) else !:IO = !.IO ).
io.write_string(fizzbuzz(N, fizz(N), buzz(N)), !IO), io.nl(!IO),
( N < To -> main(N + 1, To, !IO)
; !:IO = !.IO ).

View file

@ -0,0 +1,6 @@
1.upto(100) do |n|
print "Fizz" if a = ((n % 3) == 0)
print "Buzz" if b = ((n % 5) == 0)
print n unless (a || b)
print "\n"
end

View file

@ -1,11 +1,3 @@
1.upto(100) do |n|
print "Fizz" if a = ((n % 3) == 0)
print "Buzz" if b = ((n % 5) == 0)
print n unless (a || b)
print "\n"
end
# a little more straight forward
1.upto(100) do |n|
if (n % 15) == 0
puts "FizzBuzz"

View file

@ -0,0 +1,7 @@
(dotimes (i 100)
(println
(cond
((= 0 (% i 15)) "FizzBuzz")
((= 0 (% i 3)) "Fizz")
((= 0 (% i 5)) "Buzz")
('t i))))

View file

@ -0,0 +1,9 @@
let fizzbuzz i =
match i mod 3, i mod 5 with
0, 0 -> "FizzBuzz"
| 0, _ -> "Fizz"
| _, 0 -> "Buzz"
| _ -> string_of_int i
let _ =
for i = 1 to 100 do print_endline (fizzbuzz i) done

View file

@ -0,0 +1,9 @@
(* Useful rule declaration: "cond => f", 'cond'itionally applies 'f' to 'a'ccumulated value *)
let (=>) cond f a = if cond then f a else a
let append s a = a^s
let fizzbuzz i =
"" |> (i mod 3 = 0 => append "Fizz")
|> (i mod 5 = 0 => append "Buzz")
|> (function "" -> string_of_int i
| s -> s)

View file

@ -1,9 +0,0 @@
let output x =
match x mod 3 = 0, x mod 5 = 0 with
true, true -> "FizzBuzz"
| true, false -> "Fizz"
| false, true -> "Buzz"
| false, false -> string_of_int x
let _ =
for i = 1 to 100 do print_endline (output i) done

View file

@ -1,17 +1,15 @@
CREATE OR REPLACE PROCEDURE FIZZBUZZ AS
i NUMBER;
BEGIN
FOR i in 1 .. 100 LOOP
IF MOD(i, 15) = 0 THEN
DBMS_OUTPUT.PUT_LINE('FizzBuzz');
ELSIF MOD(i, 5) = 0 THEN
DBMS_OUTPUT.PUT_LINE('Buzz');
ELSIF MOD(i, 3) = 0 THEN
DBMS_OUTPUT.PUT_LINE('Fizz');
ELSE
DBMS_OUTPUT.PUT_LINE(i);
END IF;
END LOOP;
END FIZZBUZZ;
begin
for i in 1 .. 100
loop
case
when mod(i, 15) = 0 then
dbms_output.put_line('FizzBuzz');
when mod(i, 5) = 0 then
dbms_output.put_line('Buzz');
when mod(i, 3) = 0 then
dbms_output.put_line('Fizz');
else
dbms_output.put_line(i);
end case;
end loop;
end;

View file

@ -1,4 +1 @@
.say for
(('' xx 2, 'Fizz') xx * Z~
('' xx 4, 'Buzz') xx *) Z||
1 .. 100;
say "Fizz"x$_%%3~"Buzz"x$_%%5||$_ for 1..100

View file

@ -0,0 +1,4 @@
.say for
(('' xx 2, 'Fizz') xx * Z~
('' xx 4, 'Buzz') xx *) Z||
1 .. 100;

View file

@ -1,7 +1 @@
for n in range(1,101):
msg = ""
if not (n%3):
msg += "Fizz"
if not (n%5):
msg += "Buzz"
print msg or str(n)
for i in range(1,101): print("Fizz"*(i%3==0) + "Buzz"*(i%5==0) or i)

View file

@ -1,3 +1,13 @@
for i in range(1, 101):
words = [word for n, word in ((3, 'Fizz'), (5, 'Buzz')) if not i % n]
print ''.join(words) or i
from itertools import cycle, izip, count, islice
fizzes = cycle([""] * 2 + ["Fizz"])
buzzes = cycle([""] * 4 + ["Buzz"])
both = (f + b for f, b in izip(fizzes, buzzes))
# if the string is "", yield the number
# otherwise yield the string
fizzbuzz = (word or n for word, n in izip(both, count(1)))
# print the first 100
for i in islice(fizzbuzz, 100):
print i

View file

@ -1,6 +1,5 @@
x <- 1:100
xx <- as.character(x)
xx[x%%3==0] <- "Fizz"
xx[x%%5==0] <- "Buzz"
xx[x%%15==0] <- "FizzBuzz"
xx <- x <- 1:100
xx[x %% 3 == 0] <- "Fizz"
xx[x %% 5 == 0] <- "Buzz"
xx[x %% 15 == 0] <- "FizzBuzz"
xx

View file

@ -1,9 +1,8 @@
/*REXX program displays numbers 1 ──► 100 for the FizzBuzz problem. */
do j=1 to 100; z=j
if j//3 ==0 then z='Fizz'
if j//5 ==0 then z='Buzz'
if j//(3*5)==0 then z='FizzBuzz'
do j=1 to 100; z=j /*╔═════════════════════════════╗*/
if j//3 ==0 then z='Fizz' /*║ The IFs must be in ║*/
if j//5 ==0 then z='Buzz' /*║ ascending order.║*/
if j//(3*5)==0 then z='FizzBuzz' /*╚═════════════════════════════╝*/
say right(z,8)
end /*j*/
/*stick a fork in it, we're done.*/
end /*j*/ /*stick a fork in it, we're done.*/

View file

@ -1,11 +1,10 @@
/*REXX program displays numbers 1 ──► 100 for the FizzBuzz problem. */
do n=1 for 100
select
when n//15==0 then say 'FizzBuzz'
when n//5 ==0 then say ' Buzz'
when n//3 ==0 then say ' Fizz'
otherwise say right(n,8)
end /*select*/
end /*n*/
/*stick a fork in it, we're done.*/
do n=1 for 100
select /*╔═════════════════════════════╗*/
when n//15==0 then say 'FizzBuzz' /*║ The WHENs must be in ║*/
when n//5 ==0 then say ' Buzz' /*║ descending order║*/
when n//3 ==0 then say ' Fizz' /*╚═════════════════════════════╝*/
otherwise say right(n,8)
end /*select*/
end /*n*/ /*stick a fork in it, we're done.*/

View file

@ -1,8 +1,8 @@
/*REXX program displays numbers 1 ──► 100 for the FizzBuzz problem. */
do n=1 for 100; _=
if n//3 ==0 then _= 'Fizz'
if n//5 ==0 then _=_'Buzz'
say right(word(_ n,1),8)
end /*n*/
/*stick a fork in it, we're done.*/
do n=1 for 100; _=
if n//3 ==0 then _=_'Fizz'
if n//5 ==0 then _=_'Buzz'
/* if n//7 ==0 then _=_'Jazz' */ /*◄───note that this is a comment*/
say right(word(_ n,1),8)
end /*n*/ /*stick a fork in it, we're done.*/

View file

@ -0,0 +1,6 @@
/*REXX program displays numbers 1 ──► 100 for the FizzBuzz problem. */
/* [↓] concise & somewhat obtuse*/
do n=1 for 100
say right(word(word('Fizz',1+(n//3\==0))word('Buzz',1+(n//5\==0)) n,1),8)
end /*n*/
/*stick a fork in it, we're done.*/

View file

@ -2,5 +2,5 @@
print "Fizz" if a = (n % 3).zero?
print "Buzz" if b = (n % 5).zero?
print n unless (a || b)
print "\n"
puts
end

View file

@ -0,0 +1,8 @@
class Integer
def fizzbuzz
v = "#{"Fizz" if self % 3 == 0}#{"Buzz" if self % 5 == 0}"
v.empty? ? self : v
end
end
puts *(1..100).map(&:fizzbuzz)

View file

@ -0,0 +1,8 @@
fizzbuzz = ->(i) do
(i%15).zero? and next "FizzBuzz"
(i%3).zero? and next "Fizz"
(i%5).zero? and next "Buzz"
i
end
puts (1..100).map(&fizzbuzz).join("\n")

View file

@ -1 +1 @@
1.upto(100) { |i| p "#{[:Fizz][i%3]}#{[:Buzz][i%5]}"[/.+/m] || i }
1.upto(100) { |i| puts "#{[:Fizz][i%3]}#{[:Buzz][i%5]}"[/.+/] || i }

View file

@ -1,8 +1,5 @@
class Integer
def fizzbuzz
v = "#{"Fizz" if self % 3 == 0}#{"Buzz" if self % 5 == 0}"
v.empty? ? self : v
end
f = [nil, nil, :Fizz].cycle
b = [nil, nil, nil, nil, :Buzz].cycle
(1..100).each do |i|
puts "#{f.next}#{b.next}"[/.+/] || i
end
puts *(1..100).map(&:fizzbuzz)

View file

@ -1,8 +1,3 @@
fizzbuzz = ->(i) do
(i%15).zero? and next "FizzBuzz"
(i%3).zero? and next "Fizz"
(i%5).zero? and next "Buzz"
i
end
puts (1..100).map(&fizzbuzz).join("\n")
seq = *0..100
{Fizz:3, Buzz:5, FizzBuzz:15}.each{|k,n| n.step(100,n){|i|seq[i]=k}}
puts seq.drop(1)

View file

@ -1,20 +1,12 @@
// rust 0.8
// rust 0.13
fn main() {
let mut n:uint = 1;
while n <= 100 {
if n % 15 == 0 {
println("FizzBuzz");
}
else if n % 3 == 0 {
println("Fizz");
}
else if n % 5 == 0 {
println("Buzz");
}
else {
println(n.to_str());
}
n += 1;
for i in range(1i, 101){
let value = i.to_string();
println!("{}", match (i % 3, i % 5) {
(0,0) => "FizzBuzz",
(0,_) => "Fizz",
(_,0) => "Buzz",
_ => value.as_slice()
});
}
}

View file

@ -1,11 +1,11 @@
// rust 0.8
fn main() { for n in std::iter::range_inclusive(1,100) { fizzbuzz(n) }}
fn fizzbuzz(n:int) {
let mut buf = ~"";
if n % 3 == 0 { buf.push_str("Fizz") }
if n % 5 == 0 { buf.push_str("Buzz") }
if buf != ~"" { println!("{}", buf ) }
else { println!("{}", n ) }
// rust 0.11
fn main() {
for num in std::iter::range_inclusive(1i, 100) {
match (num % 3, num % 5) {
(0, 0) => println!("FizzBuzz"),
(0, _) => println!("Fizz"),
(_, 0) => println!("Buzz"),
(_, _) => println!("{}", num),
}
}
}

View file

@ -1,14 +1,16 @@
// rust 0.8
// rust 0.11
fn main() {
for num in std::iter::range_inclusive(1, 100) {
println(
match (num % 3, num % 5) {
(0, 0) => ~"FizzBuzz",
(0, _) => ~"Fizz",
(_, 0) => ~"Buzz",
(_, _) => num.to_str()
}
);
for num in std::iter::range_inclusive(1i, 100) {
println!("{}", fizzbuzz(num))
}
}
fn fizzbuzz(num: int) -> String {
match (num % 3, num % 5) {
(0, 0) => format!("FizzBuzz"),
(0, _) => format!("Fizz"),
(_, 0) => format!("Buzz"),
(_, _) => format!("{}", num),
}
}

View file

@ -1,14 +1,20 @@
// rust 0.8
fn main() {
for num in std::iter::range_inclusive(1, 100) {
println(
match (num % 3 == 0, num % 5 == 0) {
(false, false) => num.to_str(),
(true, false) => ~"Fizz",
(false, true) => ~"Buzz",
(true, true) => ~"FizzBuzz"
}
);
let mut n:uint = 1;
while n <= 100 {
if n % 15 == 0 {
println("FizzBuzz");
}
else if n % 3 == 0 {
println("Fizz");
}
else if n % 5 == 0 {
println("Buzz");
}
else {
println(n.to_str());
}
n += 1;
}
}

View file

@ -1,20 +1,11 @@
// rust 0.8
fn main() {
for num in range(1,101) {
let answer =
if num % 15 == 0 {
~"FizzBuzz"
}
else if num % 3 == 0 {
~"Fizz"
}
else if num % 5 == 0 {
~"Buzz"
}
else {
num.to_str()
};
println(answer);
}
fn main() { for n in std::iter::range_inclusive(1,100) { fizzbuzz(n) }}
fn fizzbuzz(n:int) {
let mut buf = ~"";
if n % 3 == 0 { buf.push_str("Fizz") }
if n % 5 == 0 { buf.push_str("Buzz") }
if buf != ~"" { println!("{}", buf ) }
else { println!("{}", n ) }
}

View file

@ -0,0 +1,14 @@
// rust 0.8
fn main() {
for num in std::iter::range_inclusive(1, 100) {
println(
match (num % 3, num % 5) {
(0, 0) => ~"FizzBuzz",
(0, _) => ~"Fizz",
(_, 0) => ~"Buzz",
(_, _) => num.to_str()
}
);
}
}

View file

@ -0,0 +1,14 @@
// rust 0.8
fn main() {
for num in std::iter::range_inclusive(1, 100) {
println(
match (num % 3 == 0, num % 5 == 0) {
(false, false) => num.to_str(),
(true, false) => ~"Fizz",
(false, true) => ~"Buzz",
(true, true) => ~"FizzBuzz"
}
);
}
}

View file

@ -0,0 +1,20 @@
// rust 0.8
fn main() {
for num in range(1,101) {
let answer =
if num % 15 == 0 {
~"FizzBuzz"
}
else if num % 3 == 0 {
~"Fizz"
}
else if num % 5 == 0 {
~"Buzz"
}
else {
num.to_str()
};
println(answer);
}
}

View file

@ -1,10 +1,8 @@
select (CASE
WHEN MOD(lvl,15)=0 THEN 'FizzBuzz'
WHEN MOD(lvl,3)=0 THEN 'Fizz'
WHEN MOD(lvl,5)=0 THEN 'Buzz'
ELSE TO_CHAR(lvl)
END) FizzBuzz
from (
select LEVEL lvl
from dual
connect by LEVEL <= 100)
SELECT CASE
WHEN MOD(level,15)=0 THEN 'FizzBuzz'
WHEN MOD(level,3)=0 THEN 'Fizz'
WHEN MOD(level,5)=0 THEN 'Buzz'
ELSE TO_CHAR(level)
END FizzBuzz
FROM dual
CONNECT BY LEVEL <= 100;

View file

@ -1,2 +1,3 @@
select nvl(decode(mod(n,3),0,'Fizz')||decode(mod(n,5),0,'Buzz'),n)
from (select level n from dual connect by level<=100)
SELECT nvl(decode(MOD(level,3),0,'Fizz')||decode(MOD(level,5),0,'Buzz'),level)
FROM dual
CONNECT BY level<=100;

View file

@ -1,7 +1,10 @@
for (x <- 1 to 100) println(
(x % 3, x % 5) match {
object FizzBuzz extends App {
1 to 100 foreach { n =>
println((n % 3, n % 5) match {
case (0, 0) => "FizzBuzz"
case (0, _) => "Fizz"
case (_, 0) => "Buzz"
case _ => x
case _ => n
})
}
}

View file

@ -1,8 +1,7 @@
def replaceMultiples(x: Int, rs: (Int, String)*) =
rs map { case (n, s) => Either cond (x % n == 0, s, x) } reduceLeft ((a, b) =>
a fold ((_ => b), (s => b fold ((_ => a), (t => Right(s + t))))))
def replaceMultiples(x: Int, rs: (Int, String)*): Either[Int, String] =
rs map { case (n, s) => Either cond(x % n == 0, s, x)} reduceLeft ((a, b) =>
a fold(_ => b, s => b fold(_ => a, t => Right(s + t))))
def fizzbuzz(n: Int) =
replaceMultiples(n, 3 -> "Fizz", 5 -> "Buzz") fold ((_ toString), identity)
def fizzbuzz = replaceMultiples(_: Int, 3 -> "Fizz", 5 -> "Buzz") fold(_.toString, identity)
1 to 100 map fizzbuzz foreach println
1 to 100 map fizzbuzz foreach println

View file

@ -1,2 +1,2 @@
def f(a: Int, b: Int, c: String, d: String): String = if (a % b == 0) c else d
for (i <- 1 to 100) println(f(i, 15, "FizzBuzz", f(i, 3, "Fizz", f(i, 5, "Buzz", i.toString))))
def f(n: Int, div: Int, met: String, notMet: String): String = if (n % div == 0) met else notMet
for (i <- 1 to 100) println(f(i, 15, "FizzBuzz", f(i, 3, "Fizz", f(i, 5, "Buzz", i.toString))))

View file

@ -1,2 +1 @@
def f(a:Int,b:Int,c:String, d:String):String = if(a % b == 0) c else d
for(i <- 1 to 100) println(f(i,15,"FizzBuzz", f(i,3,"Fizz", f(i,5,"Buzz", i.toString))))
for (n <- 1 to 100) println(List((15, "FizzBuzz"), (3, "Fizz"), (5, "Buzz")).find(t => n % t._1 == 0).getOrElse((0, n.toString))._2)

View file

@ -0,0 +1,10 @@
(define (fizzbuzz x y)
(println
(cond (( = (modulo x 15) 0 ) "FizzBuzz")
(( = (modulo x 3) 0 ) "Fizz")
(( = (modulo x 5) 0 ) "Buzz")
(else x)))
(if (< x y) (fizzbuzz (+ x 1) y)))
(fizzbuzz 1 100)

View file

@ -0,0 +1,11 @@
for i in range(1, 100)
if i % 15 == 0
echo "FizzBuzz"
elseif i % 5 == 0
echo "Buzz"
elseif i % 3 == 0
echo "Fizz"
else
echo i
endif
endfor

View file

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="utf-8"/>
<!-- Outputs a line for a single FizzBuzz iteration. -->
<xsl:template name="fizzbuzz-single">
<xsl:param name="n"/>
<!-- $s will be "", "Fizz", "Buzz", or "FizzBuzz". -->
<xsl:variable name="s">
<xsl:if test="$n mod 3 = 0">Fizz</xsl:if>
<xsl:if test="$n mod 5 = 0">Buzz</xsl:if>
</xsl:variable>
<!-- Output $s. If $s is blank, also output $n. -->
<xsl:value-of select="$s"/>
<xsl:if test="$s = ''">
<xsl:value-of select="$n"/>
</xsl:if>
<!-- End line. -->
<xsl:value-of select="'&#10;'"/>
</xsl:template>
<!-- Calls fizzbuzz-single over each value in a range. -->
<xsl:template name="fizzbuzz-range">
<!-- Default parameters: From 1 through 100 -->
<xsl:param name="startAt" select="1"/>
<xsl:param name="endAt" select="$startAt + 99"/>
<!-- Simulate a loop with tail recursion. -->
<!-- Loop condition -->
<xsl:if test="$startAt &lt;= $endAt">
<!-- Loop body -->
<xsl:call-template name="fizzbuzz-single">
<xsl:with-param name="n" select="$startAt"/>
</xsl:call-template>
<!-- Increment counter, repeat -->
<xsl:call-template name="fizzbuzz-range">
<xsl:with-param name="startAt" select="$startAt + 1"/>
<xsl:with-param name="endAt" select="$endAt"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<!-- Main procedure -->
<xsl:template match="/">
<!-- Default parameters are used -->
<xsl:call-template name="fizzbuzz-range"/>
</xsl:template>
</xsl:stylesheet>

View file

@ -0,0 +1,33 @@
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="xsl exsl">
<xsl:output method="text"/>
<xsl:template name="FizzBuzz" match="/">
<xsl:param name="n" select="1" />
<xsl:variable name="_">
<_><xsl:value-of select="$n" /></_>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($_)/_" />
<xsl:if test="$n < 100">
<xsl:call-template name="FizzBuzz">
<xsl:with-param name="n" select="$n + 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="_[. mod 3 = 0]">Fizz
</xsl:template>
<xsl:template match="_[. mod 5 = 0]">Buzz
</xsl:template>
<xsl:template match="_[. mod 15 = 0]" priority="1">FizzBuzz
</xsl:template>
<xsl:template match="_">
<xsl:value-of select="concat(.,'&#x0A;')" />
</xsl:template>
</xsl:stylesheet>

View file

@ -0,0 +1,10 @@
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of separator="&#x0A;" select="
for $n in 1 to 100 return
concat('fizz'[not($n mod 3)], 'buzz'[not($n mod 5)], $n[$n mod 15 = (1,2,4,7,8,11,13,14)])"/>
</xsl:template>
</xsl:stylesheet>