+ 4
Algorithms
Can someone guide me about algorithm basics.. finding prime numbers, LCM, HCF
2 Answers
+ 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
+ 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.