Jump to content

LCM and Prime numbers in Java

- - - - -

  • Please log in to reply
5 replies to this topic

#1
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts
I have two problems with different experiments:

First one, I know that using break; is a bad coding habit, how would I remove it from this code?
[FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Lab07A
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]static[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] main(String[] args)
{
[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Variables
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] a;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] b;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] divisor;

Scanner keyboard = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Scanner(System.in);

[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// BEGIN LAB
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Enter two integers (both > 0) to find their LCM: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);

a = keyboard.nextInt();
b = keyboard.nextInt();

divisor = 1;

[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (divisor <= a * b)
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (divisor % a == 0 && divisor % b == 0)
{
System.out.println(a + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" and "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + b + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" have LCM: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + divisor);
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]break[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New];
}
divisor++;
}
}
}[/FONT]

Second, I need to print all the prime numbers between the two inputted integers. I got it to work with smaller integers, but once it goes over 100's it just does everything wrong, so how would I rewrite this to do it correctly?
[FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] java.util.Scanner;

[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Lab07B
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]static[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] main(String[] args)
{
[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Variables
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] start;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] end;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] found_divisor_count = 0;
[/FONT][FONT=Courier New]Scanner keyboard = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Scanner(System.in);

[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// BEGIN LAB
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"What is the start interger: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
start = keyboard.nextInt();

System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"What is the end integer (greater than start): "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
end = keyboard.nextInt();

[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (start <= end)
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (start % 2 != 0 && start % 3 != 0 || start == 3)
{
System.out.println(start + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" is a prime number."[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
}
start++;
}
}
}
[/FONT]
[FONT=Courier New]
[/FONT]

---------- Post added at 10:49 AM ---------- Previous post was at 09:43 AM ----------

EDIT:
Just added some code to the Primenumbers, is there a shorter block of code?
[FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] java.util.Scanner;

[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Lab07B
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]static[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] main(String[] args)
{
[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Variables
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] start;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] end;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] found_divisor_count = 0;

Scanner keyboard = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Scanner(System.in);

[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// BEGIN LAB
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"What is the start interger: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
start = keyboard.nextInt();

System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"What is the end integer (greater than start): "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
end = keyboard.nextInt();

[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (start <= end)
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (start%2 != 0 && start%3 != 0 && start%4 != 0 && start%5 != 0 && start%7 != 0 || start == 2 || start == 3 || start == 5 || start == 7)
{ 
System.out.println(start + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" is a prime number."[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
}
start++;
}


}
}
[/FONT]


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
First: You could use a flag to indicate you've found the divisor, but I'd probably do this:

[FONT=Courier New]divisor = 1;  [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (divisor <= a * b && divisor % a != 0 && divisor % b  != 0) { [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][/FONT][/COLOR][/FONT][FONT=Courier New]divisor++; } [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (divisor % a == 0 && divisor % b == 0) { System.out.println(a + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" and "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + b + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" have LCM: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + divisor); [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][/FONT][/COLOR][/FONT][FONT=Courier New]} [/FONT] 

Second: unless I missed something, your logic will claim 35, 55, and 77 are prime. You need to check against all known primes less than the current value. Google sieve of eratosthenes
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts

WingedPanther said:

First: You could use a flag to indicate you've found the divisor, but I'd probably do this:

[FONT=Courier New]divisor = 1;  [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (divisor <= a * b && divisor % a != 0 && divisor % b  != 0) { [/FONT][FONT=Courier New]divisor++; } [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (divisor % a == 0 && divisor % b == 0) { System.out.println(a + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" and "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + b + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" have LCM: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + divisor); [/FONT][FONT=Courier New]} [/FONT]

Second: unless I missed something, your logic will claim 35, 55, and 77 are prime. You need to check against all known primes less than the current value. Google sieve of eratosthenes
Prime number do work but this is the readme for the second lab

Lab 07B: find all the prime numbers between two numbers entered. Suppose
we call them 'start' and 'end.' You can assume 'end' larger than 'start.'
For example: if 12 and 44 are entered into your program, your program
will display: 13 17 19 23 29 31 37 41.

Hint: a nested double while loop. The outer loop cycles through all the
numbers to be checked. The inner loop checks on the number to see if it
is a prime number. A prime number x can only be evenly divided by 1 and
itself (cannot be evenly divided by any numbers ranging from 2 to x - 1.

assume: x entered is the number to go thru a series of tests
with different divisors: 1, 3, 4, ..., x - 1, x

set found_divisor_count to 0 (to count how many divisors can evenly
divide x)

loop thru all the divisors (1 2 3 ... x):
if x can be evenly divided by the current divisor
-> add 1 to the found_divisor_count
end loop

if the found_divisor_count is 2
-> x is prime
(else
-> x is not prime)

The above tests only one number (x). We need to test a range of numbers,
between the two integers entered (start, end, both > 1).

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I personally don't think a break is bad. If you don't like it, then either use an extra boolean like winged suggested. Or else put the loop in a method, and return instead of breaking.
Note you can even use "return" if the method returns void.

public void something(){
   for(String word : words){
        if(word.equals("SearchThis"){
              System.out.println("Got it");
              return;
        }
   }
}


#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I would do sieve of eratosthenes, and simply use the start/end values to indicate when to start output and stop looping.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts
   import java.util.Scanner;


   public class Lab07A
   {
      public static void main(String[] args)
      {
      // Variables
         int a;
         int b;
         int divisor;
      
         Scanner keyboard = new Scanner(System.in);
      
      // BEGIN LAB
         System.out.print("Enter two integers (both > 0) to find their LCM: ");
      
         a = keyboard.nextInt();
         b = keyboard.nextInt();
      
         divisor = 1;
      
         while (a%divisor == 0 && b%divisor == 0 && divisor != 1)
         {
            divisor = (a/divisor) * b;
            System.out.println("The LCM of " + a + " and " + b + " is " + divisor);
         }
         //divisor++;
      }
   }

If I input 20 for the first, and 35 for the second, and set the divisor to 5, then it works perfectly. I just need to fix it so it can increment. I also got the second lab to work, for those who are interested.


   import java.util.Scanner;


   public class Lab07B
   {
      public static void main(String[] args)
      {
      // Variables
         int start;
         int end;
         int found_divisor_count = 0;
         int x;//
         int divisor;
      
         Scanner keyboard = new Scanner(System.in);
      
      // BEGIN LAB
         System.out.print("What is the start interger (> 1): ");
         start = keyboard.nextInt();
      
         System.out.print("What is the end integer (> start): ");
         end = keyboard.nextInt();
      
          
         x = start;
         while (x <= end)
         {
            divisor = 1;
            while (divisor <= x)
            {
               if (x%divisor == 0)
               {
                  found_divisor_count++;
               }
               divisor++;
            }
            if (found_divisor_count == 2)
            {
               System.out.println(x + " is prime");
            }
            found_divisor_count = 0;
            
            x++;
         }


      }
   }





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users