Factors are the numbers that can divide any given integer completely. Prime numbers have 2 factors only, 1 and itself. There are certain observations regarding factors.
- 1 and the digit itself will always be the factor.
- Factors always come in pairs, for example, 1*60, 2*30, 3*20, etc
- The largest factor of any digit is the digit itself.
- 2nd largest factor will be N/2, the 3rd largest factor is N/3 and the 4th largest factor is N/4, and so on.
- Factors will always be less than the square root of the number.
package org.wesome.dsalgo;
public class CheckPrime {
public static boolean checkPrime(int prime) {
if (prime <= 1) {
System.out.println("1 is smallest prime number");
return true;
}
for (int i = 2; i * i <= prime; i++) {
if (prime % i == 0) {
return false;
}
}
return true;
}
}
package org.wesome.dsalgo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CheckPrimeTest {
@Test
void checkPrimeTest1() {
Assertions.assertFalse(CheckPrime.checkPrime(10));
}
@Test
void checkPrimeTest2() {
Assertions.assertTrue(CheckPrime.checkPrime(17));
}
}
plugins {
id 'java'
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}
test {
useJUnitPlatform()
}