Python Chocolates By Numbers
from math import gcd
def chocolates_by_numbers(n: int, m: int) -> int:
return (n * m) // gcd(n, m) // m
This uses the greatest common divisor to figure out how many chocolates get eaten before the pattern repeats.
Rust Chocolates By Numbers
fn gcd(n: i64, m: i64) -> i64 {
if n % m == 0 { m } else { gcd(m, n % m) }
}
fn chocolates_by_numbers(n: i64, m: i64) -> i64 {
(n * m) / gcd(n, m) / m
}
This uses the greatest common divisor to figure out how many chocolates get eaten before the pattern repeats.
TypeScript Chocolates By Numbers
function chocolatesByNumbers(n: number, m: number): number {
const gcd = (x: number, y: number): number => (x % y === 0 ? y : gcd(y, x % y));
return (n * m) / gcd(n, m) / m;
}
This uses the greatest common divisor to figure out how many chocolates get eaten before the pattern repeats.
Bash Common Prime Divisors
gcd() {
local _n=$1 _m=$2
if (( _n % _m == 0 )); then
echo "$_m"
return
fi
gcd "$_m" $(( _n % _m ))
}
remove_common_prime_divisors() {
local _n=$1 _m=$2 _d
while (( _n != 1 )); do
_d=$(gcd "$_n" "$_m")
if (( _d == 1 )); then
break
fi
_n=$(( _n / _d ))
done
echo "$_n"
}
common_prime_divisors() {
local -n _arrA="$1"
local -n _arrB="$2"
local _counter=0 _i _x _y _d _rx _ry
for _i in "${!_arrA[@]}"; do
_x=${_arrA[$_i]}
_y=${_arrB[$_i]}
_d=$(gcd "$_x" "$_y")
_rx=$(remove_common_prime_divisors "$_x" "$_d")
if (( _rx != 1 )); then
continue
fi
_ry=$(remove_common_prime_divisors "$_y" "$_d")
if (( _ry == 1 )); then
((_counter++))
fi
done
echo "$_counter"
}
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
C++ Common Prime Divisors
#include <cstddef>
#include <vector>
long long commonPrimeDivisorsGcd(long long n, long long m)
{
if (n % m == 0) {
return m;
}
return commonPrimeDivisorsGcd(m, n % m);
}
long long removeCommonPrimeDivisors(long long n, long long m)
{
while (n != 1) {
long long d = commonPrimeDivisorsGcd(n, m);
if (d == 1) {
break;
}
n /= d;
}
return n;
}
int commonPrimeDivisors(const std::vector<int>& a, const std::vector<int>& b)
{
int counter = 0;
for (std::size_t i = 0; i < a.size(); ++i) {
long long x = a[i];
long long y = b[i];
long long d = commonPrimeDivisorsGcd(x, y);
x = removeCommonPrimeDivisors(x, d);
if (x != 1) {
continue;
}
y = removeCommonPrimeDivisors(y, d);
if (y == 1) {
++counter;
}
}
return counter;
}
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
C# Common Prime Divisors
static int CommonPrimeDivisors(int[] a, int[] b)
{
long Gcd(long n, long m) => n % m == 0 ? m : Gcd(m, n % m);
long RemoveCommonPrimeDivisors(long n, long m)
{
while (n != 1)
{
var d = Gcd(n, m);
if (d == 1)
{
break;
}
n /= d;
}
return n;
}
var counter = 0;
for (int i = 0; i < a.Length; i++)
{
long x = a[i];
long y = b[i];
var d = Gcd(x, y);
x = RemoveCommonPrimeDivisors(x, d);
if (x != 1)
{
continue;
}
y = RemoveCommonPrimeDivisors(y, d);
if (y == 1)
{
counter++;
}
}
return counter;
}
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
Elixir Common Prime Divisors
defmodule CommonPrimeDivisors do
def common_prime_divisors(a, b) do
a
|> Enum.zip(b)
|> Enum.count(fn {x, y} ->
d = gcd(x, y)
remove_common(x, d) == 1 and remove_common(y, d) == 1
end)
end
defp gcd(n, m) when rem(n, m) == 0, do: m
defp gcd(n, m), do: gcd(m, rem(n, m))
defp remove_common(1, _m), do: 1
defp remove_common(n, m) do
d = gcd(n, m)
if d == 1, do: n, else: remove_common(div(n, d), m)
end
end
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
Erlang Common Prime Divisors
-module(common_prime_divisors).
-export([common_prime_divisors/2]).
common_prime_divisors(A, B) ->
length([ok || {X, Y} <- lists:zip(A, B), has_same_prime_divisors(X, Y)]).
has_same_prime_divisors(X, Y) ->
D = gcd(X, Y),
case remove_common(X, D) of
1 -> remove_common(Y, D) =:= 1;
_ -> false
end.
remove_common(1, _) -> 1;
remove_common(N, M) ->
case gcd(N, M) of
1 -> N;
D -> remove_common(N div D, M)
end.
gcd(N, M) when N rem M =:= 0 -> M;
gcd(N, M) -> gcd(M, N rem M).
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
Go Common Prime Divisors
func commonPrimeDivisors(a, b []int) int {
var gcd func(n, m int) int
gcd = func(n, m int) int {
if n%m == 0 {
return m
}
return gcd(m, n%m)
}
removeCommonPrimeDivisors := func(n, m int) int {
for n != 1 {
d := gcd(n, m)
if d == 1 {
break
}
n /= d
}
return n
}
counter := 0
for i := range a {
x, y := a[i], b[i]
d := gcd(x, y)
x = removeCommonPrimeDivisors(x, d)
if x != 1 {
continue
}
y = removeCommonPrimeDivisors(y, d)
if y == 1 {
counter++
}
}
return counter
}
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
Haskell Common Prime Divisors
commonPrimeDivisors :: [Int] -> [Int] -> Int
commonPrimeDivisors as bs = length (filter matches (zip as bs))
where
matches (x, y) =
let d = gcd x y
in strip x d == 1 && strip y d == 1
strip n m
| n == 1 = 1
| d == 1 = n
| otherwise = strip (n `div` d) m
where
d = gcd n m
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.