Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FlourPacker {
- public static void main(String[] args) {
- System.out.println(getLargestPrime(217));
- }
- public static int getLargestPrime(int number) {
- int toReturn = 0;
- if (number <= 1) {
- return -1;
- } else {
- // checking if given number is the prime number
- boolean isPrime = false;
- int count = 0;
- for(int i = number; i > 0; i--) {
- if(number % i == 0) {
- count++;
- if(count > 2) {
- isPrime = false;
- break;
- }
- else if(count == 2) {
- isPrime = true;
- }
- }
- }
- if(isPrime) {
- toReturn = number;
- } else {
- int count2 = 0;
- for(int i = (number - 1); i > 0; i--) {
- if(number % i == 0) {
- // below checking if devider is the prime number
- count2 = 0;
- for(int x = i; x > 0; x--) {
- if (i % x == 0) {
- count2++;
- }
- }
- if(count2 == 2) {
- int multiplier = 1;
- int toCompare = 0;
- while(multiplier * i <= number)
- if(multiplier * i == number) {
- toCompare = i;
- if(toCompare > toReturn) {
- toReturn = i;
- break;
- } else {
- break;
- }
- } else {
- multiplier++;
- }
- }
- }
- }
- }
- }
- return toReturn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement