'Check if number is prime in class constructor?
I would like to check when instantiating a object if the input are valid or not (prime); within the constructor parameters of the class. How would I accomplish this in typescript? I have already created a lambda and static function for checking if a variable is prime, which one would be best to use in the constructor method? Here is my code:
class RSA {
public check = (n: number) => {
if (n <= 1)
return false;
var i: number = 2;
while(i*i <= n)
n%i == 0 ? false : i += 1;
return n;
}
/**
* Create a new RSA object
* @param p
* @param q
* @returns object capable of encryption/decryption
*/
constructor(p: number, q: number) {
this.generateKeys(p, q);
}
/**
* Returns boolean to verify if 'n' is prime
* @param n input to verify (e.g. p and q)
* @returns boolean of n
*/
public static isPrime(n: number): number | boolean {
if (n <= 1)
return false;
var i: number = 2;
while(i*i <= n)
n%i == 0 ? false : i += 1;
return n;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
