If you’re new to programming but want a career in tech, I HIGHLY RECOMMEND applying to one of Springboard’s online coding bootcamps (use code ALEXLEE for $1,000 off): bit.ly/3HX970h
Instead of n/2, I think you could instead use the square root of 'n', rounded up (to the next integer) if the decimal value is greater than zero. This is because any number that's bigger than the square root will have to be multiplied by a SMALLER number than the square root, in order for the product of these two numbers to equal the number in question. Also, you can start the count at ONE, and then count by 2 (counting only odd numbers). So, instead of "i++", you would have "i+=2".
the face cam helps me keep focus, the keyboard cam helps to see exactly what youre doing and the shortcut cam is like a mini tip and tricks by itself. combine that with great videos like these and you got yourself a winning playlist for learning java basics
this is a great method but how would we account for the fact that 1 and 0 are not prime numbers? the method would return true for those integers otherwise
hi there, i tried to apply this in intellij and it refused to take start, end ,...and other namings....many errors in the code. anything better than this ?
Simply put, you actually cant call a non-static method inside a static method. So in this case the main method is always static here he has no choice but to findPrimes static. He would have to make an entirely new class then create an object of it like a class Called PrimeCalculation that has the non-static method findPrimes. Then he would have to do PrimeCalculation.findPrimes(1,100); inside of this file he is showing obviously that is a lot of work for no benefit. Additionally, Static means its not part of an object meaning it can be used right away without making an object. So pros of static methods: Pros: Easier to use since you don't need to use objects and other classes, better for simple things, sometimes you only want to do a simple task and not have it a part of an object Cons: You can't use it on object instances, kind of like a 1 time use thing(the other times you use it essentially get overridden) Hence this kind of defeats the purpose of object oriented programming. Although for simple demonstrations it does not matter much :)
Hey Omur, you are correct. One is not a prime number, though it meets the criteria. All we have to do is put an extra line of code in, a conditional, just before the while loop. if (n == 1) prime = false; Then all is well.
my professor gave us an exam and we all failed because she didnt teach a program that doesnt use import java.util.*Scanner; , and then the question is give atleast 3 examples and make a program that can detect the lowest number without using a scanner. I really need help and i dont know what to do.
n/2 is nearly the largest factor of n and there r no more factors after that For example: if n=32 it's largest factor is 32÷2=16. There's no point in checking the numbers after 16
If you’re new to programming but want a career in tech, I HIGHLY RECOMMEND applying to one of Springboard’s online coding bootcamps (use code ALEXLEE for $1,000 off): bit.ly/3HX970h
gotta love how fast programs can go. one million numbers in 16 seconds! holy crap!
Instead of n/2, I think you could instead use the square root of 'n', rounded up (to the next integer) if the decimal value is greater than zero.
This is because any number that's bigger than the square root will have to be multiplied by a SMALLER number than the square root, in order for the product of these two numbers to equal the number in question.
Also, you can start the count at ONE, and then count by 2 (counting only odd numbers). So, instead of "i++", you would have "i+=2".
the face cam helps me keep focus, the keyboard cam helps to see exactly what youre doing and the shortcut cam is like a mini tip and tricks by itself. combine that with great videos like these and you got yourself a winning playlist for learning java basics
That was an awesome program ... definitely one of the harder ones i found, but i got there in the end!
THANK YOU SM.
btw your hands are too fast 😭💗 i'm trying to reach this level in writing
Thank you you saved my life crying emoji
Thank u so much it's the exact video that I was looking for....
I love your keyboard. Where can I get one?
public static boolean isPrime(final long n)
{
if (n == 2) return true;
if ((n < 2) || (n % 2 == 0)) return false;
for (long i = 3; i < (long) Math.floor(Math.sqrt(n)) + 1; i += 2)
{if (n % i == 0) return false;}
return true;
}
Thank you so much Alex 🙏🏼
Great video, just:
if (n == 1) {
return false;
}
Thank You!
this is a great method but how would we account for the fact that 1 and 0 are not prime numbers? the method would return true for those integers otherwise
for (int>2)
so you put nothing for n, so then how does the while loop run? n is never defined
Hey I was just wondering, How would you make a loop using strings?
I dont think its possible
hi there, i tried to apply this in intellij and it refused to take start, end ,...and other namings....many errors in the code. anything better than this ?
Great thx alot.
is there a video out there about finding the largest number?
Is there a video that explains why you made the method static? I am still struggling with when/why to use static methods. Thank you!
Simply put, you actually cant call a non-static method inside a static method. So in this case the main method is always static here he has no choice but to findPrimes static.
He would have to make an entirely new class then create an object of it like a class Called PrimeCalculation that has the non-static method findPrimes. Then he would have to do PrimeCalculation.findPrimes(1,100); inside of this file he is showing obviously that is a lot of work for no benefit.
Additionally, Static means its not part of an object meaning it can be used right away without making an object.
So pros of static methods:
Pros: Easier to use since you don't need to use objects and other classes, better for simple things, sometimes you only want to do a simple task and not have it a part of an object
Cons: You can't use it on object instances, kind of like a 1 time use thing(the other times you use it essentially get overridden) Hence this kind of defeats the purpose of object oriented programming. Although for simple demonstrations it does not matter much :)
1 is not a prime number, there is an error with the code.
Hey Omur, you are correct. One is not a prime number, though it meets the criteria. All we have to do is put an extra line of code in, a conditional, just before the while loop.
if (n == 1) prime = false; Then all is well.
@@陈瀚龙 Or:
if (prime && n != 1)
instead of just: if (prime)
what to do is if I want to print a statement only once I while using a for loop???
my professor gave us an exam and we all failed because she didnt teach a program that doesnt use import java.util.*Scanner; , and then the question is give atleast 3 examples and make a program that can detect the lowest number without using a scanner. I really need help and i dont know what to do.
Where you get that keyboard from ?
Thank yōu
what is the type of your keyboard?
how do you print just 10 prime number per line
Can u do How much Pilot G Tec c4 can write
how to write prime numbers in revers order
Why using n/2
Love from india
1 isn't a prime number
why n/2 ?
n/2 is nearly the largest factor of n and there r no more factors after that
For example: if n=32 it's largest factor is 32÷2=16.
There's no point in checking the numbers after 16
i'm tryin to do it to 500,000, but nothing running
from 2 to 500,000
@@chinecheremejikeme6209 it takes a while for the program to be executed with such big numbers.
It's datatype will be change u have to use long then instead of int
one is not a prime number :)
.