In here, we are checking whether the number is divisble by itself or 1. If a divides num, there must be b. n/a = b n = a * b Example - Consider number 36 - Find all the factors of 36 that exits in pairs - { a, b}, - {1,36} - {2,18} - {3, 12} - {4, 9} -{ 6, 6} -------------- (a, b} are same. sqrt(n) Pairs are repeated, except a and b positions are swapped. - { 9, 4} - {12, 3} - {18, 2} - {36, 1} {6,6} {a,b} => a^2 = n Therefore, a = sqrt(n) if a < b , a < sqrt(n) , b > sqrt(n) if a > b, a > sqrt(n), b < sqrt(n) From factor pairs of 36, you can see that if you cannot find the factor until sqrt(n), you will not be able to find factors after sqrt of n. Therefore in here Math.sqrt(num) is more efficient compared to num/2 particularly for larger number. Hope this helps.
Smart Programming, so you learn from other videos? I have seen some of your videos in Java and I immediately thought you are an expert. You know all the details behind java language.
Hey buddy, thank you for making this video! For (i < Math.sqrt(n)), I think it must be (i < (Math.sqrt(n) + 1)), so that an i value of Math.sqrt(n) is checked for (n % i) inside the loop. Also, a number less than 2 should be checked for (if(n < 2) { isPrime = false; break;}), because 1 is not a prime number and the for() loop begins with "int i = 2". Otherwise, great video!
@@swarnimkoteshwar basic mathematics. To check if a number is prime, all you have to do is to check if any number between 2 and the square root of the number( the one you're checking if its a prime or not) divides evenly into it. Example, square root of 16 is 4, to check if 16 is a prime, you have to check (16 % (1 to 4) )=0. If yes, it is not a prime.
Sir thanx for this course! it really help me and improve my skills to achieve my task,sir can you make more videos about Swing components like JPopupMenu JMenuItem KeyStrokes KeyEvent i littlebit confuse with these components
Telusko, you haven't explained why we use break in this case which is very important. When you use break at the moment the number is successfully divided the operation is stopped an you spend less time. Everyone who do not understand why we have to use it in this case try this code with and without "break;". public class PrimeNumber { /** * @param args the command line arguments */ public static void main(String[] args) {
long startTime = System.currentTimeMillis(); int num = 250000000;
boolean isPrime = true;
for(int i = 2; i < num; i++){ if ( num % i == 0 ){ isPrime = false; break; } } if (isPrime) { System.out.println("number is prime");} else { System.out.println("Number is NOT prime"); } long stopTime = System.currentTimeMillis(); long elapsedTime = stopTime - startTime; System.out.println("Elapsed time is : " + elapsedTime); }
You so much brilliant . Please work with java(OOP) in a Project in which we can easily understand how and when works Inheritance,encaptulation,polymorphism,abstraction ,inheritance and others built in methods from begining to advanced . We will be more greatfull to u.
import java.util.Scanner; public class JavaProgram { public static void main(String args[]) { int num, i, count=0; Scanner scan = new Scanner(System.in); System.out.print("Enter a Number : "); num = scan.nextInt(); for(i=2; i
sir there is also an another way to do this.. class prime_number { public static void main(String[] args) { int num=100; if(num%2!=0 && num%3!=0 && num%5!=0 && num%7!=0) { System.out.println("prime"); } else { System.out.println("not prime"); }
You have not put braces after else. So the break statement is not in the else block and will be executed in the first iteration itself and the loop will terminate.
Here is the correct solution that covers negative as well as number 2 : import java.util.Scanner; public class NumIsPrimeOrNot { public static void main(String[] args) { //Question: is given number is prime or not? return boolean //Prime check: //1) number should be Natural number > 1 (Note: 1 is not a prime but 2 is prime number //2) number should have only 2 factors. Means divisible by only 1 and the number itself. Scanner sc = new Scanner(System.in); System.out.println("Enter any number : "); int num = sc.nextInt(); boolean isPrime = true; if (num
At line 12, i,e num%i , i is not decleared as datatype as locally . It shows error. But if declear as local or globally then error shows in break statement. WHY??? Please explain.
29 is a prime number when the loop counter reaches 29. But for you have put a condition if(number%i==0){isprime = false; break;} so this is a problem my dear sir.
import java.util.Scanner; public class PrimeNumber { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int num=sc.nextInt(); for (int i = 2; i
Let me share with you guys what I've come up with after a while of coding and debugging. Let me know if this was helpful. public class Is_NPrime { public static int IsPrime (int N) { int Dividers = 0; int ReturnValue = 1;
for (int i = 1; i < N; i++) { if (N % i == 0) Dividers ++;
if (Dividers > 1) // Break from the loop as soon as possible!! { ReturnValue = 0; break; } }
The true art of coding is how you can code things in a easy way so that humans can also understand. And what you try to do here is just making it more complex. So keep this in mind and try to make things easy.
If you say num/2 than you have to say i
In here, we are checking whether the number is divisble by itself or 1.
If a divides num, there must be b.
n/a = b
n = a * b
Example - Consider number 36
- Find all the factors of 36 that exits in pairs
- { a, b},
- {1,36}
- {2,18}
- {3, 12}
- {4, 9}
-{ 6, 6} -------------- (a, b} are same. sqrt(n)
Pairs are repeated, except a and b positions are swapped.
- { 9, 4}
- {12, 3}
- {18, 2}
- {36, 1}
{6,6} {a,b} => a^2 = n
Therefore,
a = sqrt(n)
if a < b , a < sqrt(n) , b > sqrt(n)
if a > b, a > sqrt(n), b < sqrt(n)
From factor pairs of 36, you can see that if you cannot find the factor until sqrt(n), you will not be able to find factors after sqrt of n. Therefore in here Math.sqrt(num) is more efficient compared to num/2 particularly for larger number. Hope this helps.
nice tutorial sir, some of them are so helpful, thanks and keep it up 👍👍
Smart Programming, so you learn from other videos?
I have seen some of your videos in Java and I immediately thought you are an expert. You know all the details behind java language.
.
Hey buddy, thank you for making this video! For (i < Math.sqrt(n)), I think it must be (i < (Math.sqrt(n) + 1)), so that an i value of Math.sqrt(n) is checked for (n % i) inside the loop. Also, a number less than 2 should be checked for (if(n < 2) { isPrime = false; break;}), because 1 is not a prime number and the for() loop begins with "int i = 2". Otherwise, great video!
it will be better if you explain reasoing behind why restrict loop till num/2 OR Math.sqrt(num) ...thx for sharing .keep it up..
ikrrr
@@swarnimkoteshwar basic mathematics. To check if a number is prime, all you have to do is to check if any number between 2 and the square root of the number( the one you're checking if its a prime or not) divides evenly into it. Example, square root of 16 is 4, to check if 16 is a prime, you have to check (16 % (1 to 4) )=0. If yes, it is not a prime.
Thank you so much! This is the exact way I wrote the code in java for prime no. ❤️❤️ Thanks!
It has a bug, if someone enter 2 as value it will return as not prime
Sir thanx for this course! it really help me and improve my skills to achieve my task,sir can you make more videos about Swing components like JPopupMenu JMenuItem KeyStrokes KeyEvent i littlebit confuse
with these components
Telusko, you haven't explained why we use break in this case which is very important. When you use break at the moment the number is successfully divided the operation is stopped an you spend less time. Everyone who do not understand why we have to use it in this case try this code with and without "break;". public class PrimeNumber {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int num = 250000000;
boolean isPrime = true;
for(int i = 2; i < num; i++){
if ( num % i == 0 ){
isPrime = false;
break;
}
}
if (isPrime)
{
System.out.println("number is prime");}
else
{
System.out.println("Number is NOT prime");
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Elapsed time is : " + elapsedTime);
}
}
this is the most helpful vid I found to solve this problem. thank u so much sir
BRO I have been trying to find a way to do this for class. thank you for the understandable walk thru
You so much brilliant . Please work with java(OOP) in a Project in which we can easily understand how and when works Inheritance,encaptulation,polymorphism,abstraction ,inheritance and others built in methods from begining to advanced . We will be more greatfull to u.
import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int num, i, count=0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Number : ");
num = scan.nextInt();
for(i=2; i
what is count
why u useed count++ when u have i++ over here
If we use i
Clean Explanation
sir there is also an another way to do this..
class prime_number
{
public static void main(String[] args) {
int num=100;
if(num%2!=0 && num%3!=0 && num%5!=0 && num%7!=0)
{
System.out.println("prime");
}
else
{
System.out.println("not prime");
}
In your video could you please check with number 25 instead of 29 using Math.sqrt(num), for me when i run your program its showing 25 is prime number.
correct
He forgot to write equal (=) sign
It must be i
Excellent video.
what if the case when user want to give an input which is a 3 digit no. then this code won't work
sir your volume so slow and i don't understand boolean value work in this program. Sir please fully explain boolean.
int x=290; boolean var=true;
for(int i=1; i
1.09
public class PrimeNumberCheck
{
public static void main(String[] args)
{
int number;
number=12;
for(int i=2;i
in this case it will print as many times as loop iterates ... so to prevent this he uses boolean function
maybe to print true or false.. but it can also be done like " Number is NOT Prime hence false". still ya ur right
You have not put braces after else. So the break statement is not in the else block and will be executed in the first iteration itself and the loop will terminate.
First of all your coding is wrong , 2 is a prime number
U r right
thank u good explanation
Excellent video! Great explanation! Keep up the outstanding work!👌👏👍
sir, by this concept can we print the random 10 numbers and check it whether its a prime number or not
Why you take onece true and false??
thank you for this lecture lots of thanks
Here is the correct solution that covers negative as well as number 2 :
import java.util.Scanner;
public class NumIsPrimeOrNot {
public static void main(String[] args) {
//Question: is given number is prime or not? return boolean
//Prime check:
//1) number should be Natural number > 1 (Note: 1 is not a prime but 2 is prime number
//2) number should have only 2 factors. Means divisible by only 1 and the number itself.
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number : ");
int num = sc.nextInt();
boolean isPrime = true;
if (num
Thanks sir
but what if the number is 1 or less ?
Can I make a program for testing by only using for loop ?
while using Math.sqrt , it should be i
Abhilash Potharaju it would actually print any square of a prime as a prime.
At line 12, i,e num%i , i is not decleared as datatype as locally . It shows error. But if declear as local or globally then error shows in break statement. WHY??? Please explain.
29 is a prime number when the loop counter reaches 29. But for you have put a condition if(number%i==0){isprime = false; break;} so this is a problem my dear sir.
Poor sound quality. Always speak louder
Thanks
Sir I think it ll not work for 2 .
int k=1 ,
n=7, sum=0;
while(k
very helpful
I think it should be i
navin scond code(For (int i = 2 ; i < num/2 ; i++)
{
Is prime = false;
})and the third one (For (int i = 2 ; i
The condition should be i
sir i need your help i want to print prime number between 1 to 100
package Prime_Number;
public class PrimeNumber {
public static void main(String[] args) {
int number=100;
for (int i=2;i
@@sonusharma6580 galat hai
can you keep vedios of selection sort,insertion sort and bubble sort....... can you upload please its urgent for me
What if we give input as 0 . It will give result as prime
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int num=sc.nextInt();
for (int i = 2; i
Why flag is used ?
sir, what is dead code in java
What about time complexity???
After declare n/2 in for loop its showing 4 is a prime number
what is the purpose of break statement here ?
because if it gets divide then it will return false and thats what we want. n after getting that why to go further simply BREAK it n will come out
What is abt package com.navin ???
sir you are teaching as if people already knows java.
these videos are not helpful to those who doesnt kows java..
You must go through this book for complete knowledge in java "Complete refrence in Java"
Let me share with you guys what I've come up with after a while of coding and debugging. Let me know if this was helpful.
public class Is_NPrime
{
public static int IsPrime (int N)
{
int Dividers = 0;
int ReturnValue = 1;
for (int i = 1; i < N; i++)
{
if (N % i == 0)
Dividers ++;
if (Dividers > 1) // Break from the loop as soon as possible!!
{
ReturnValue = 0;
break;
}
}
return ReturnValue;
}
public static void main(String[] args)
{
System.out.println (IsPrime (12));
}
}
The true art of coding is how you can code things in a easy way so that humans can also understand. And what you try to do here is just making it more complex. So keep this in mind and try to make things easy.
Sir if the number is 2?
does isPrime built in function
"isprime" is Boolean type variable
You can change it to "primeis" or anything you wanna use ^-^
👌👌👌
I didn't understand what % doing?
That is mod..
Remainder when dividing
eg : - 5 % 2 = 1
why did you put i++
to go for every no upto 25
nice program and coding application link send
Public class prime
{
public static void main(string[] args)
{
int n=20;
If(n%2==0)
System.out.println("even");
else
System.out.println("odd");
}
}
plzzzzz improve your audio its very low audiable
❤
Very dirty sound quality!!!
poor sound quality
Worst method 😢😢😢😢
bruh...u just called us guyz... i didnt expect this frm u vro..(no one cares lol)
bakvas
Thank you