+ 4

Algorithms

Can someone guide me about algorithm basics.. finding prime numbers, LCM, HCF

27th Jan 2018, 3:12 PM
Grey Coder
Grey Coder - avatar
2 Réponses
+ 14
u can make your own algorithms ... just see what u need to find & what methods u know ... make use of them calculating hcf will be easy ... just find common factors in both & for lcm=product of no.'s /hcf of no.'s
27th Jan 2018, 4:15 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
To test whether a number is prime try every number from 2 to sqrt(n). If any of them divides n, n is not prime. Greatest common divisor can be found by Euclidean algorithm. gcd(a,b) is the same as gcd(a-b,b), when a>b. if a==b, you have found it LCM is a*b/gcd(a,b). Further you can look up binary search or some graph algorithms, like DFS.
27th Jan 2018, 3:36 PM
michal