first commit
This commit is contained in:
6
FizzBuzz/Ada/fizzbuzz.adb
Normal file
6
FizzBuzz/Ada/fizzbuzz.adb
Normal file
@@ -0,0 +1,6 @@
|
||||
with Ada.Text_IO;
|
||||
|
||||
procedure FizzBuzz is
|
||||
begin
|
||||
Ada.Text_IO.Put_Line("Hello, world!");
|
||||
end FizzBuzz;
|
||||
33
FizzBuzz/C/CMakeLists.txt
Normal file
33
FizzBuzz/C/CMakeLists.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# Modify only these if one source file!
|
||||
project(CFizzBuzz)
|
||||
set(CURRENT_PROJECT_CODE_NAME fizzbuzz)
|
||||
set(FILE_EXT c)
|
||||
# End
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED True)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
# We want all the warnings and as errors enabled
|
||||
if (MSVC)
|
||||
# warning level 4 and all warnings as errors
|
||||
add_compile_options(/W4 /WX)
|
||||
else()
|
||||
# lots of warnings and all warnings as errors
|
||||
add_compile_options(-Wall -Wextra -pedantic -Werror)
|
||||
endif()
|
||||
|
||||
add_executable(${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/${CURRENT_PROJECT_CODE_NAME}.${FILE_EXT})
|
||||
|
||||
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_INCLUDES}
|
||||
)
|
||||
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_LIBS}
|
||||
)
|
||||
|
||||
|
||||
132
FizzBuzz/C/fizzbuzz.c
Normal file
132
FizzBuzz/C/fizzbuzz.c
Normal file
@@ -0,0 +1,132 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
void fizzbuzz(int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
if (i%3 == 0 || i%5 == 0)
|
||||
{
|
||||
if (i % 3 == 0 && i % 5 == 0)
|
||||
printf("fizzbuzz\n");
|
||||
else if (i%3==0)
|
||||
printf("fizz\n");
|
||||
else
|
||||
printf("buzz\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d\n", i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define BUF_SIZE 1024
|
||||
|
||||
void fizzbuzz_fast(int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
char str[BUF_SIZE];
|
||||
int curr_str_pos = 0;
|
||||
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
if (i % 3 == 0 || i % 5 == 0)
|
||||
{
|
||||
char* strToPrint;
|
||||
int result;
|
||||
|
||||
if (i % 3 == 0 && i % 5 == 0)
|
||||
{
|
||||
strToPrint = "fizzbuzz\n";
|
||||
}
|
||||
else if (i % 3 == 0)
|
||||
{
|
||||
strToPrint = "fizz\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
strToPrint = "buzz\n";
|
||||
}
|
||||
|
||||
result = snprintf(str + curr_str_pos, BUF_SIZE - curr_str_pos, "%s", strToPrint);
|
||||
if (!(result < BUF_SIZE - curr_str_pos))
|
||||
{
|
||||
printf("%s", str);
|
||||
curr_str_pos = 0;
|
||||
result = snprintf(str + curr_str_pos, BUF_SIZE - curr_str_pos, "%s", strToPrint);
|
||||
}
|
||||
curr_str_pos += result;
|
||||
}
|
||||
else
|
||||
{
|
||||
int result;
|
||||
result = snprintf(str + curr_str_pos, BUF_SIZE - curr_str_pos, "%d\n", i);
|
||||
|
||||
if (!(result < BUF_SIZE - curr_str_pos))
|
||||
{
|
||||
printf("%s", str);
|
||||
curr_str_pos = 0;
|
||||
result = snprintf(str + curr_str_pos, BUF_SIZE - curr_str_pos, "%d\n", i);
|
||||
}
|
||||
curr_str_pos += result;
|
||||
}
|
||||
}
|
||||
|
||||
if (curr_str_pos != 0)
|
||||
{
|
||||
printf("%s", str);
|
||||
}
|
||||
}
|
||||
|
||||
void fizzbuzz_branchless(int n)
|
||||
{
|
||||
char *zzc[]={"fizz\n","buzz\n","fizzbuzz\n","%d\n"};
|
||||
int zxc[]={3,0,0,1,0,0,2};
|
||||
for (int i = 1; i < n; ++i)
|
||||
{
|
||||
int a=i%3==0;
|
||||
int b=i%5==0;
|
||||
int t=b^a;
|
||||
printf(zzc[zxc[a<<2|(b<<1)|t]],i);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
int n;
|
||||
|
||||
clock_t reg_start, reg_end, branchless_start, branchless_end, fast_start, fast_end;
|
||||
|
||||
printf("How many fizzbuzzes?: ");
|
||||
int ret = scanf("%d", &n);
|
||||
if (ret)
|
||||
{
|
||||
reg_start = clock();
|
||||
fizzbuzz(n);
|
||||
reg_end = clock();
|
||||
|
||||
branchless_start = clock();
|
||||
fizzbuzz_branchless(n);
|
||||
branchless_end = clock();
|
||||
|
||||
fast_start = clock();
|
||||
fizzbuzz_fast(n);
|
||||
fast_end = clock();
|
||||
|
||||
printf("regular time: %ld\n", reg_end - reg_start);
|
||||
printf("branchless time: %ld\n", branchless_end - branchless_start);
|
||||
printf("fast time: %ld\n", fast_end - fast_start);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Invalid input\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
4
FizzBuzz/CMakeLists.txt
Normal file
4
FizzBuzz/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/C/CMakeLists.txt)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/Cpp/CMakeLists.txt)
|
||||
39
FizzBuzz/CSharp/fizzbuzz.cs
Normal file
39
FizzBuzz/CSharp/fizzbuzz.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
namespace LanguageBasics
|
||||
{
|
||||
class CSharpFizzBuzz
|
||||
{
|
||||
static void FizzBuzz(int n)
|
||||
{
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
if (i%3==0 || i%5==0)
|
||||
{
|
||||
if (i%3==0)
|
||||
{
|
||||
System.Console.Write("Fizz");
|
||||
}
|
||||
if (i%5==0)
|
||||
{
|
||||
System.Console.Write("Buzz");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Console.Write(i);
|
||||
}
|
||||
System.Console.WriteLine("");
|
||||
}
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
System.Console.WriteLine("How many fizzbuzzes?: ");
|
||||
string input = System.Console.ReadLine();
|
||||
|
||||
if (System.Int32.TryParse(input, out int num))
|
||||
FizzBuzz(num);
|
||||
else
|
||||
System.Console.WriteLine("Invalid input");
|
||||
}
|
||||
}
|
||||
}
|
||||
33
FizzBuzz/Cpp/CMakeLists.txt
Normal file
33
FizzBuzz/Cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# Modify only these if one source file!
|
||||
project(CppFizzBuzz)
|
||||
set(CURRENT_PROJECT_CODE_NAME fizzbuzz)
|
||||
set(FILE_EXT cpp)
|
||||
# End
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED True)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
# We want all the warnings and as errors enabled
|
||||
if (MSVC)
|
||||
# warning level 4 and all warnings as errors
|
||||
add_compile_options(/W4 /WX)
|
||||
else()
|
||||
# lots of warnings and all warnings as errors
|
||||
add_compile_options(-Wall -Wextra -pedantic -Werror)
|
||||
endif()
|
||||
|
||||
add_executable(${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/${CURRENT_PROJECT_CODE_NAME}.${FILE_EXT})
|
||||
|
||||
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_INCLUDES}
|
||||
)
|
||||
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_LIBS}
|
||||
)
|
||||
|
||||
|
||||
32
FizzBuzz/Cpp/fizzbuzz.cpp
Normal file
32
FizzBuzz/Cpp/fizzbuzz.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
|
||||
void fizzbuzz(int n)
|
||||
{
|
||||
for (auto i = 1; i <= n; i++)
|
||||
{
|
||||
if (i%3 == 0 || i%5 == 0)
|
||||
{
|
||||
if (i%3 == 0)
|
||||
std::cout << "fizz";
|
||||
if (i%5 == 0)
|
||||
std::cout << "buzz";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << i;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
std::cout << "How many fizzbuzzes?: ";
|
||||
std::cin >> n;
|
||||
if (!std::cin.fail())
|
||||
fizzbuzz(n);
|
||||
else
|
||||
printf("Invalid input\n");
|
||||
return 0;
|
||||
}
|
||||
35
FizzBuzz/D/fizzbuzz.d
Normal file
35
FizzBuzz/D/fizzbuzz.d
Normal file
@@ -0,0 +1,35 @@
|
||||
import std.stdio;
|
||||
|
||||
void fizzBuzz(int n)
|
||||
{
|
||||
for (int i = 1; i<= n; i++)
|
||||
{
|
||||
if (i%3==0 || i%5==0)
|
||||
{
|
||||
if (i%3==0)
|
||||
write("fizz");
|
||||
if (i%5==0)
|
||||
write("buzz");
|
||||
}
|
||||
else
|
||||
{
|
||||
write(i);
|
||||
}
|
||||
writeln("");
|
||||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
int i;
|
||||
write("How many fizzbuzzes?: ");
|
||||
try {
|
||||
readf("%d", i);
|
||||
fizzBuzz(i);
|
||||
} catch (Exception e)
|
||||
{
|
||||
writeln("Invalid input");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
37
FizzBuzz/Fortran/fizzbuzz.f90
Normal file
37
FizzBuzz/Fortran/fizzbuzz.f90
Normal file
@@ -0,0 +1,37 @@
|
||||
subroutine fizzbuzzsub(n)
|
||||
implicit none
|
||||
integer, intent(in) :: n
|
||||
integer :: i
|
||||
|
||||
do i = 1, n
|
||||
if ((modulo(i,3) == 0) .OR. (modulo(i,5) == 0)) then
|
||||
if (mod(i,3)==0) then
|
||||
write(*, "(a)", advance="no") "Fizz"
|
||||
end if
|
||||
if (mod(i,5)==0) then
|
||||
write(*, "(a)", advance="no") "Buzz"
|
||||
end if
|
||||
else
|
||||
write(*, "(i0)", advance="no") i
|
||||
end if
|
||||
print *,""
|
||||
end do
|
||||
|
||||
|
||||
|
||||
end subroutine fizzbuzzsub
|
||||
|
||||
program fizzbuzz
|
||||
implicit none
|
||||
integer :: n
|
||||
integer :: Reason
|
||||
|
||||
print *, "How many fizzbuzzes?: "
|
||||
read (*,*,IOSTAT=Reason) n
|
||||
|
||||
if (Reason > 0) then
|
||||
print *, "Invalid input"
|
||||
else
|
||||
call fizzbuzzsub(n)
|
||||
end if
|
||||
end program fizzbuzz
|
||||
2
FizzBuzz/Haskell/fizzbuzz.hs
Normal file
2
FizzBuzz/Haskell/fizzbuzz.hs
Normal file
@@ -0,0 +1,2 @@
|
||||
main :: IO()
|
||||
main = putStrLn "Hello, World!"
|
||||
40
FizzBuzz/Java/fizzbuzz.java
Normal file
40
FizzBuzz/Java/fizzbuzz.java
Normal file
@@ -0,0 +1,40 @@
|
||||
class fizzbuzz
|
||||
{
|
||||
private static void fizzBuzz(int n)
|
||||
{
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
if (i%3 == 0 || i%5 == 0)
|
||||
{
|
||||
if (i%3 == 0)
|
||||
System.out.print("fizz");
|
||||
if (i%5 == 0)
|
||||
System.out.print("buzz");
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.print(i);
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
// Your program begins with a call to main().
|
||||
// Prints "Hello, World" to the terminal window.
|
||||
public static void main(String args[])
|
||||
{
|
||||
System.out.print("How many fizzbuzzes?: ");
|
||||
String input = System.console().readLine();
|
||||
|
||||
try
|
||||
{
|
||||
int n = Integer.parseInt(input);
|
||||
fizzBuzz(n);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("Invalid input");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
FizzBuzz/Python/fizzbuzz.py
Normal file
18
FizzBuzz/Python/fizzbuzz.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import sys
|
||||
|
||||
try:
|
||||
N = int(input("How many fizzbuzzes?: "))
|
||||
except:
|
||||
print("Invalid input")
|
||||
sys.exit()
|
||||
|
||||
for x in range(1,N+1):
|
||||
if x % 3 == 0 or x % 5 == 0:
|
||||
if x % 3 == 0:
|
||||
print("fizz", end="")
|
||||
if x % 5 == 0:
|
||||
print("buzz", end="")
|
||||
else:
|
||||
print(x, end="")
|
||||
print("")
|
||||
|
||||
29
FizzBuzz/Rust/fizzbuzz.rs
Normal file
29
FizzBuzz/Rust/fizzbuzz.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
fn fizzbuzz(n: u32) {
|
||||
for i in 1..=n {
|
||||
if i%3==0 || i%5==0
|
||||
{
|
||||
if i%3==0 {
|
||||
print!("fizz");
|
||||
}
|
||||
if i%5==0 {
|
||||
print!("buzz");
|
||||
}
|
||||
}
|
||||
else {
|
||||
print!("{}",i);
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("How many fizzbuzzes? ");
|
||||
|
||||
let mut input_text = String::new();
|
||||
std::io::stdin().read_line(&mut input_text).expect("Failed to read from stdin");
|
||||
let trimmed = input_text.trim();
|
||||
match trimmed.parse::<u32>() {
|
||||
Ok(i) => fizzbuzz(i),
|
||||
Err(..) => println!("Invalid input"),
|
||||
};
|
||||
}
|
||||
5
FizzBuzz/problem.txt
Normal file
5
FizzBuzz/problem.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
/*
|
||||
Write a program that prints the numbers 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”.
|
||||
*/
|
||||
Reference in New Issue
Block a user