[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology

Name
Options
Comment
Verification
4chan Pass users can bypass this verification. [Learn More] [Login]
File
  • Please read the Rules and FAQ before posting.
  • You may highlight syntax and preserve whitespace by using [code] tags.

08/21/20New boards added: /vrpg/, /vmg/, /vst/ and /vm/
05/04/17New trial board added: /bant/ - International/Random
10/04/16New board for 4chan Pass users: /vip/ - Very Important Posts
[Hide] [Show All]


[Advertise on 4chan]


File: fizz-buzz-2.png (17 KB, 1280x1024)
17 KB PNG
How do you solve it efficiently without the use of a modulo operator.
>>
divide to decimal and check if its a whole number
>>
just make two resetting counters

fizzbuzz literally was just a test designed to see if you're able to do the absolute basics of programming and solve a really simple problem yourself, not your modulo knowledge
>>
>>109570561
>>109570570
Thats a slimy way to solve it, you are doing modulo repackaged.
>>
>>109570549
study number theory op
>>
>>109570549
>cycle efficiency
Write one full iteration of the pattern into the program memory as an array, but contents are "functions" (just inline it in practice, but consider them functions on the higher level of abstraction) that take a base number and return an offset from that number (e.g. x + 1, or fizz/buzz for the appropriate steps of the pattern). Your main loop iterates over the array of functions, then increments the base number by the length of the pattern.
The base number can sit in a secondary register since it doesn't need to be updated that often, so the assembly code is just loading a value into the primary register, adding the secondary register (if your assembly language doesn't let you do that in one step...), and writing it out to console. Plus a few diversions into whatever the string handling code looks like.
>(program) memory efficiency / lines of code
Just do what this anon >>109570570 said
>>
>>109570826
>so the assembly code is just loading a value into the primary register
Oh, right, or you can just use increments since the values will always be sequential (except for the gaps created by fizz/buzz outputs)
>>
>>109570570
>just make two resetting counters
No. Just use a single resetting counter for a pattern that repeates after 15 steps.
The you optimize for lines 1, 3, and 5 that all have n, n+1, fizz and move that to a subroutine.
>>
>>109570549
I make the ai do it while I think what I will cook for dinner
>>
I_k(n) = (1/k) * SUM_{m=0..k-1} e^(2 * pi * i * m * n / k)

- If k divides n --> I_k(n) = 1
- If k !divides n --> I_k(n) = 0


>Example 1: k = 3 ("Fizz")
The 3rd roots of unity are z_0 = 1, z_1 = -0.5 + i*(√3/2), z_2 = -0.5 - i*(√3/2)

Testing n = 3 (Divisible):
(z_0)^3 = 1^3 = 1
(z_1)^3 = e^(i * 2π) = 1
(z_2)^3 = e^(i * 4π) = 1

I_3(3) = (1/3) * (1 + 1 + 1) = 3/3 = 1 --> "Fizz"

Testing n = 2 (Not Divisible):
(z_0)^2 = 1
(z_1)^2 = -0.5 - i*(√3/2)
(z_2)^2 = -0.5 + i*(√3/2)

I_3(2) = (1/3) * (1 - 0.5 - 0.5 + 0i) = 0 --> No "Fizz"


>Example 2: k = 5 ("Buzz")
The 5th roots of unity form a 5 pointed regular star/pentagon around the complex unit circle spaced at 72°

Testing n = 5 (Divisible):
Every root (z_m)^5 = e^(i * 2π * m) = 1
I_5(5) = (1/5) * (1 + 1 + 1 + 1 + 1) = 5/5 = 1 --> "Buzz"

Testing n = 1 (Not Divisible):
Summing the vertices of a centered 5-gon:
Re = 1 + 2*cos(72°) + 2*cos(144°) = 1 + (√5-1)/2 - (√5+1)/2 = 0
Im = sin(72°) - sin(72°) + sin(144°) - sin(144°) = 0

I_5(1) = (1/5) * (0 + 0i) = 0 --> No "Buzz"
>>
>>109570622
That's like saying division is just multiplication repackaged just because you can do *0.5 instead of /2, you fucking insufferable faggot bastard.
>>
>>109570549
In c++26, they made this very simple:
#include <print>
#include <utility>

template<class... Ts>
struct overloads : Ts...
{
using Ts::operator()...;
};

int
main()
{
[&]<auto... i>(std::index_sequence<i...>) {
[]<auto offset, auto... ps>(this auto self) -> void {
static constexpr auto process_one = []<auto... is> {
((overloads{
[]<std::size_t n>(std::integral_constant<std::size_t, n>) -> void { std::print("{}\n", n + is...[0] - 1); },
[](std::integral_constant<std::size_t, 3>) -> void { std::print("fizz\n"); },
[](std::integral_constant<std::size_t, 5>) -> void { std::print("buzz\n"); },
[](std::integral_constant<std::size_t, 6>) -> void { std::print("fizz\n"); },
[](std::integral_constant<std::size_t, 9>) -> void { std::print("fizz\n"); },
[](std::integral_constant<std::size_t, 10>) -> void { std::print("buzz\n"); },
[](std::integral_constant<std::size_t, 12>) -> void { std::print("fizz\n"); },
[](std::integral_constant<std::size_t, 15>) -> void { std::print("fizzbuzz\n"); },
}
.template operator()(std::integral_constant<std::size_t, is - is...[0] + 1>{})),
...);
};

if constexpr (sizeof...(ps) - offset >= 15) {
[&]<auto... k>(std::index_sequence<k...>) {
process_one.template operator()<ps...[offset + k]...>();
}(std::make_index_sequence<15>{});

self.template operator()<offset + 15, ps...>();
} else if constexpr (sizeof...(ps) - offset > 0) {
[&]<auto... k>(std::index_sequence<k...>) {
process_one.template operator()<ps...[offset + k]...>();
}(std::make_index_sequence<sizeof...(ps) - offset>{});
}
}.template operator()<0, (i+1)...>();
}(std::make_index_sequence<100>{});
}



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.