Fancy DSA Fancy DSA
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

想的更多,也要想的更远
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • Roadmap 路线
  • 算法简介
  • Sort 排序

  • Search 搜索

  • Recursive 递归

  • Graph 图

  • Tree 树

  • Math 数学

    • Abs [绝对值]
    • AliquotSum [真因数和]
    • Area [面积]
    • ArithmeticGeometricMean [算术-几何平均数]
    • ArmstrongNumber [阿姆斯特朗数]
    • AverageMean [平均数]
    • AverageMedian [中位数]
    • BinaryExponentiation [二分求幂]
    • BisectionMethod [二分法]
    • BinaryConvert [二进制转换]
    • CheckKishnamurthyNumber [克里希纳穆尔西数]
    • Coordinate
    • CoPrimeCheck
      • 实现
    • DecimalIsolate
    • DegreeToRadian
    • DecimalExpansion
  • Hash 哈希

  • String 字符串

  • BitManipulation 位操纵

  • Backtracking 回溯

  • DynamicProgramming 动态规划

  • Cache 缓存

  • Array 数组

  • Ciphers 密码学

  • Conversions 转换

  • ProjectEuler 欧拉计划

  • 其他

  • 算法
  • Math 数学
jonsam
2022-09-26
目录

CoPrimeCheck

# 实现

# JavaScript

/*
    Problem statement and Explanation : https://en.wikipedia.org/wiki/Coprime_integers

    In number theory, two integers a and b are coprime, relatively prime or
    mutually prime if the only positive integer that is a divisor of both
    of them is Consequently, any prime number that divides one of a
    or b does not divide the other. This is equivalent to their greatest
    common divisor (gcd) being. One says also a is prime to b or a
    is coprime with b.
*/

// Here we use a GetEuclidGCD method as a utility.
const GetEuclidGCD = (arg1, arg2) => {
  let less = arg1 > arg2 ? arg2 : arg1
  for (less; less >= 2; less--) {
    if ((arg1 % less === 0) && (arg2 % less === 0)) return (less)
  }
  return (less)
}

// CoPrimeCheck function return the boolean in respect of the given number is co-prime or not.
/**
 * CoPrimeCheck function return the boolean in respect of the given number is co-prime or not.
 * @param {Number} firstNumber first number for checking is prime or not.
 * @param {Number} secondNumber second number for checking is prime or not.
 * @returns return correspond boolean value, if both number are co-prime return `true`, else return `false`.
 */
const CoPrimeCheck = (firstNumber, secondNumber) => {
  // firstly, check that input is a number or not.
  if (typeof firstNumber !== 'number' || typeof secondNumber !== 'number') {
    return new TypeError('Argument is not a number.')
  }
  /*
    This is the most efficient algorithm for checking co-primes
    if the GCD of both the numbers is 1 that means they are co-primes.
    */
  return GetEuclidGCD(firstNumber, secondNumber) === 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
编辑 (opens new window)
上次更新: 2022/10/18, 22:58:49
Coordinate
DecimalIsolate

← Coordinate DecimalIsolate→

最近更新
01
0-20题解
10-31
02
本章导读
10-31
03
算法与转换:Part1
10-28
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy DSA | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式