QUICK

QUICK

YOU HAVE TEN SECONDS TO WRITE A FUNCTION THAT CALCULATES THE LOWEST COMMON MULTIPLE OF TWO INTEGERS (>= 1)

OR THIS BIRD IS GONNA STAB YOU

Attached: 1520888289684.jpg (412x430, 24K)

Other urls found in this thread:

scastie.scala-lang.org/6NEcj7iNRYiWtpXP1GsPAA
twitter.com/SFWRedditGifs

C MASTER RACE

#include
#include

int gcd(int a, int b) {
while(a != b) {
if(a > b) {
a -= b;
} else {
b -= a;
}
}

return a;
}

int lcm(int a, int b) {
return a * b / gcd(a, b);
}

int main() {
printf("%d\n", lcm(21, 6));

return EXIT_SUCCESS;
}

int lcm(int a, int b){
return Math.lcm(a,b);
}

BIRDMIN NO

DON'T DISAPPOINT ME Jow Forums
GET TO FUCKING WORK

Attached: angrybird.jpg (600x475, 42K)

I have two favourite languages.
#include
#include

auto lcm(uint a, uint b) -> decltype(a*b)
{
return std::lcm(a, b);
}

auto main() -> int
{
std::cout u32 {
let mut result = a*b;
for n in a.. {
if n*a % b == 0 {
result = n*a;
break;
}
}
result
}

fn main() {
println!("{}", lcm(7, 21));
}

Attached: 1518057870451.jpg (986x1188, 325K)

>auto

Attached: 1516237770805.jpg (598x800, 62K)

dont ask Jow Forums to do your homework u faggit

def lcm(a, b):
lower_bound = min(a, b)
upper_bound = a * b + 1
return next(m for m in range(lower_bound, upper_bound) if a % m == b % m == 0)

Attached: maxresdefault.jpg (1920x1080, 125K)

ah fuck, it should actually be if m % a == m % b == 0

>a..
1..a*

You know these mean the same thing, right?
auto foo() -> int;
int foo();

No I want to use decltype umu

It's not required for main

Where's your std::consistency?

Attached: 1522736757941.png (451x619, 429K)

I only use things created for templates, when using templates

Built in solution:
*.

Fun and cool solution:
([: >./ ,:)&.(_&q:)

akarin a shit

delit

Attached: 3453.png (213x230, 41K)

no u

Also, the LCM of a list of integers is
>./&.(_&q:)

Kinda funny that it's shorter

I'm a software engineer, not a mathematician.

and?

void lcm2intgte1() { return 1; }

ok man I'll give it a try (not tested and not even a written in any particular language)

const int num1;
const int num2;
const int iter;
int answer;

if (num1>num2)
iter = num1;
else
iter = num2;

for (int i = iter; while i > 0; i--) {
if (num1 % i == 0 & num2 % i == 0)
answer = i;
}

print(i);

Got a few errors but meh, I can live with that.

gogogo Javascript
comments and criticism welcome, feel free to subscribe to my blog

let LCM=function(int1,int2){
let max=Math.max(int1,int2);
for(i=max+1;;i++){
if(i%int1===0 && i%int2===0){
return i;
}
}
}

You mean the highest common multiple?
(define (gcd a b)
(if (= (mod a b) 0)
b
(gcd b (mod a b)) ))
Simple.

>C fags really are this dumb

fun gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
fun lcm(a: Int, b: Int) = a * b / gcd(a, b)

(kotlin)

>OP asks for lowest common multiple
>thinks highest common multiple
>posts greatest common multiple

I'm not used to these weird terms. He's asking for the lowest common DENOMINATOR? Okay, that adds one line:
(define (lcd a b) (/ (* a b) (gcd a b)))

didn't you ask this question in a thread last week, except you fucked up and asked for "given two integers A and B, the integer if A % B == 0"

peak Jow Forums

this is bird of perforations or practice, that was probably a kid asking for homework help in engrish

Why'd you post an image of a small portion of a room?

Also, all these brute force iteration methods disgust me. Do you guys implement b^n by multiplying n times?

The horror.

return a*b;

#include //std::common_type
template
constexpr std::common_type_t gcd(M m, N n){
if(m>n){
if(n==0) return m;
return gcd(n,m%n);
}
else{
if(m==0) return n;
return gcd(m,n%m);
}
}
template
constexpr M gcd(M m){
return m;
}
template
constexpr auto gcd(M m, N n, V... vs) -> decltype(auto){
return gcd(gcd(m,n), vs...);
}
template
constexpr T lcm(T t) {
return t;
}
template
constexpr auto lcm(T t, U u, V... vs) ->decltype(auto) {
return lcm(t*(u/gcd(t,u)), vs...);
}

int lcm(int a, int b) {
return a * b / gcd(a, b);
}

c++17 has built in gcd now, and you could always use __gcd

LCM(int x, int y)
{
int i = 1;
while(true){
if(i % x == 0 && i % y == 0)
return i;
i++;
}
}

PLEASE DON'T STAB ME BIRD

why would you start from i=1?
what if the integers are really large

BECAUSE THE BIRD ONLY GAVE ME 10 SECONDS AND WAS GOING TO STAB ME.

What the actual shit

def lcm(num1,num2):
a = []
b = []
place = 1
while True:
for i in range(20):
a.append(place*num1)
b.append(place*num2)
place += 1
for product in a:
if product in b:
return product

Type in "efficient lcm calculation" into google.
Press Enter
Do that.

Birb, I would if I wasn't so busy, sorry :(

Provide example cases to test against. (This would also help anons that do not know what we need to find and are too lazy to google)

typical brainlets

TEN SECONDS ISNT ENOUGH TIME BIRD DROP IT PLEASE

Attached: Ohdear.png (1366x768, 93K)

lel

YOUR FATE IS SEALED

Attached: 16-crows (1).jpg (605x454, 104K)

rest in piece

>Enter a number:3
>Divisible by 2

You got stabbed to fuck. When your programming is so bad it breaks math, you need to re-evaluate.

>if x % 2 and y % 2 == 0

Attached: apu-giggles.png (710x577, 30K)

YOUR PUNISHMENT MUST BE MORE SEVERE

Attached: Bird-Sword-4da478b07ac4b_hires.jpg (800x900, 47K)

I don't really know python, and I can't really think clearly at the moment, but
if x % 2 and y % 2 == 0:

only evaluate to true when y is divisible by 2, but x isn't.

>THE LOWEST COMMON MULTIPLE
wtf is dat

it should be if x % 2 == 0 and y % 2 == 0:

def gcd(a: Int, b: Int) : Int = if (b == 0) a else gcd(b, a % b)
def lcm(a: Int, b: Int) = a / gcd(a,b) * b


scastie.scala-lang.org/6NEcj7iNRYiWtpXP1GsPAA

I am really bad at programming..

#include
#include
#include

using namespace std;

vector Factor(int x){
vector buff;
int tmp = x;
while(x % 2 == 0){
buff.push_back(2);
x = x/2;
}
for(int i = 3; i

>goto

Attached: 1504151903748.jpg (403x403, 27K)

ES6 masterrace
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));

//Could do a single-line arrow function, but readable code is better for shit threads like this.
const lcm = (x, y) => {
if(x === 0 || y === 0)
return 0;

let denom = gcd(Math.abs(x), Math.abs(y));
return Math.abs(Math.floor(x / denom) * y);
}

i didnt copy this from stdlib
proc gcd*[T](x, y: T): T =
var (x,y) = (x,y)
while y != 0:
x = x mod y
swap x, y
abs x

proc lcm*[T](x, y: T): T = x div gcd(x, y) * y

lol fags
#include

int lcm(int a, int b)
{
int res = 1;
int i = 2;
while(a > 1 && b > 1) {
while(a % i == 0 || b % i == 0) {
res = res * i;
a = (a % i == 0) : (a / i) ? a;
b = (b % i == 0) : (b / i) ? b;
}
i++;
}

return res;
}

fight me.

>could do a single-line arrow function
>single-line arrow function
what

Attached: 1508954432600.jpg (846x960, 112K)

OKAY OKAY OKAY

int lcm_fast(int x, int y){
int n = max(x,y);
for(int i = 1; (n%min(x,y)); i++) n = i*max(x,y);
return n;
}

const lcm = (x, y) =>
(x === 0 || y === 0) ? 0 : Math.abs(Math.floor(x / gcd(Math.abs(x), Math.abs(y))) * y);

Pretty sure that is all kinds of wrong.

how are those "-> x"s called? im learning c++ desu

int lcm (int a, int b) {
int n;
for (n = a; n % a != 0 || n % b != 0; n++);
return n;
}

>...

Probably a copycat. The original programming bird always posts a reference solution in C right at the start.

please explain

Attached: 21371351631.jpg (600x497, 20K)

>^ because you did not adhere to proper programming procedure (i.e. 0 commenting) you get cuts. GET EM BIRDY.


>^ only one who goes home unhurt CONGRATS

Attached: 0009 - SCG8lgv.jpg (650x482, 309K)

Wow, on second look, I think this actually works but should run like ass for numbers with large prime factors.

Write your own gcd, lazy cunt.

Instead of incrementing from a, why not multiply like ? Also, you're assuming a is the larger of the two instead of testing.

Does that garbage even work? I'm not reading ~100 LoC for something that should only take 4-8.
Lots of stabbin' this evening.

Stupid frog poster.

OH YES user PLEASE HURT MY FEELS MORE.

Attached: 0098 - VWHodZx.png (400x400, 8K)

>comments
>one line function

kek

Built into Smalltalk:
15 lcm: 20

lcm(A, B, X) :- A>B, lcm_(B, A, X, 0).
lcm(A, B, X) :- lcm_(A, B, X, 0).
lcm_(A, B, X, R) :- RR is R+A, 0 is mod(RR, B), X is RR.
lcm_(A, B, X, R) :- RR is R+A, lcm_(A, B, X, RR).

There's something rather elegant about this one, even though single line for loops are pig disgusting and it's not very efficient. Still, it strikes a good balance.

Everyone mocks you behind your back.

>using namespace std;

Write the liar paradox in your favorite language. Go.

What is the second language?

Looks like go

def scm(a, b):
while a != b:
if a < b: a, b = b, a
a = a - b
return a


Was that 10 seconds?

# Euclides' algorithm
def scm(a, b):
# End condition of Euclides' algo
while a != b:
# Flip a and b if a < b such that a > b
if a < b: a, b = b, a
a = a - b
return a


Commented

bump

Yeah it works. It's just that I didn't want to brute force testing every multiple, and I didn't know there was a formula of a*b/gcd(a,b).

It prime factorizes each number, counts up the occurence of the prime factors and multiplies all the prime factors together the number of times they appear the most in either of the factored numbers. You can just run it, it works.

>and I didn't know there was a formula of a*b/gcd(a,b).
Neither did I, I just thought about it for like three seconds after realizing he was asking for lowest common denominator, not greatest common divisor. Kinda obvious that the lcd is just (a*b)/gcd(a,b), and I'm too much of a mathlet to even know how to prove that. You could get the gcd using your alg by storing a separate variable that is only changed when both a%i and b%i are zero; it's just the product of all the factors shared between a and b, after all. Seeing other methods that get the same answer in different ways is actually pretty enlightening.

def scm(a, b):
while a != b:
if a < b: a, b = b, a
a = a - b
return a

return 1

>Write your own gcd, lazy cunt.
See , I already did.