Prime Number Program in Java #62

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ก.ย. 2024

ความคิดเห็น • 45

  • @alexlorenlee
    @alexlorenlee  10 หลายเดือนก่อน +2

    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

  • @CollidaCube
    @CollidaCube 4 ปีที่แล้ว +17

    gotta love how fast programs can go. one million numbers in 16 seconds! holy crap!

  • @mattsmith6828
    @mattsmith6828 3 ปีที่แล้ว +9

    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".

  • @somename3424
    @somename3424 2 ปีที่แล้ว +1

    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

  • @vishy
    @vishy 4 ปีที่แล้ว +11

    That was an awesome program ... definitely one of the harder ones i found, but i got there in the end!

  • @strangeussser
    @strangeussser 4 ปีที่แล้ว +8

    THANK YOU SM.
    btw your hands are too fast 😭💗 i'm trying to reach this level in writing

  • @jadrienclemente8157
    @jadrienclemente8157 4 ปีที่แล้ว +7

    Thank you you saved my life crying emoji

  • @Abhaykumar-pi8ng
    @Abhaykumar-pi8ng 3 ปีที่แล้ว

    Thank u so much it's the exact video that I was looking for....

  • @markrosadino2702
    @markrosadino2702 4 ปีที่แล้ว +2

    I love your keyboard. Where can I get one?

  • @mattsmith6828
    @mattsmith6828 3 ปีที่แล้ว +1

    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;
    }

  • @sirmohammed772
    @sirmohammed772 4 ปีที่แล้ว

    Thank you so much Alex 🙏🏼

  • @shotx333
    @shotx333 2 ปีที่แล้ว

    Great video, just:
    if (n == 1) {
    return false;
    }

  • @asadbekbozorov5714
    @asadbekbozorov5714 3 ปีที่แล้ว

    Thank You!

  • @scaramoucheysimp
    @scaramoucheysimp 3 ปีที่แล้ว +1

    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

  • @ozzDeveveloperOpenForWork
    @ozzDeveveloperOpenForWork ปีที่แล้ว

    so you put nothing for n, so then how does the while loop run? n is never defined

  • @gabrielcantu3209
    @gabrielcantu3209 5 ปีที่แล้ว +2

    Hey I was just wondering, How would you make a loop using strings?

    • @funlife5946
      @funlife5946 ปีที่แล้ว

      I dont think its possible

  • @aksel8034
    @aksel8034 2 ปีที่แล้ว

    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 ?

  • @yyyyyyyyyyy1988
    @yyyyyyyyyyy1988 2 ปีที่แล้ว

    Great thx alot.

  • @ChMaDutton
    @ChMaDutton 2 ปีที่แล้ว

    is there a video out there about finding the largest number?

  • @michellereckling7495
    @michellereckling7495 2 ปีที่แล้ว

    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!

    • @kakashi99908
      @kakashi99908 2 ปีที่แล้ว +1

      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 :)

  • @omursahin7280
    @omursahin7280 4 ปีที่แล้ว +2

    1 is not a prime number, there is an error with the code.

    • @陈瀚龙
      @陈瀚龙 4 ปีที่แล้ว +1

      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.

    • @maelstrom57
      @maelstrom57 3 ปีที่แล้ว +1

      @@陈瀚龙 Or:
      if (prime && n != 1)
      instead of just: if (prime)

  • @samayra0012
    @samayra0012 3 ปีที่แล้ว

    what to do is if I want to print a statement only once I while using a for loop???

  • @ninjacat1653
    @ninjacat1653 4 ปีที่แล้ว

    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.

  • @user-xe5uc4ex3u
    @user-xe5uc4ex3u 3 ปีที่แล้ว

    Where you get that keyboard from ?

  • @ankush_in_sync5998
    @ankush_in_sync5998 3 ปีที่แล้ว

    Thank yōu

  • @66albukhari
    @66albukhari 3 ปีที่แล้ว

    what is the type of your keyboard?

  • @sungnguyen3920
    @sungnguyen3920 3 ปีที่แล้ว

    how do you print just 10 prime number per line

  • @ghettoasfuckxp
    @ghettoasfuckxp 5 ปีที่แล้ว

    Can u do How much Pilot G Tec c4 can write

  • @ahavlogsandfashions3994
    @ahavlogsandfashions3994 3 ปีที่แล้ว

    how to write prime numbers in revers order

  • @vinothr5024
    @vinothr5024 3 ปีที่แล้ว

    Why using n/2

  • @thelionhour8907
    @thelionhour8907 3 ปีที่แล้ว

    Love from india

  • @regomar
    @regomar 9 หลายเดือนก่อน

    1 isn't a prime number

  • @shivpatel8288
    @shivpatel8288 4 ปีที่แล้ว

    why n/2 ?

    • @dinakaremmanuel1723
      @dinakaremmanuel1723 4 ปีที่แล้ว +9

      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

  • @chinecheremejikeme6209
    @chinecheremejikeme6209 3 ปีที่แล้ว

    i'm tryin to do it to 500,000, but nothing running

    • @chinecheremejikeme6209
      @chinecheremejikeme6209 3 ปีที่แล้ว

      from 2 to 500,000

    • @szirakiakos4334
      @szirakiakos4334 3 ปีที่แล้ว

      @@chinecheremejikeme6209 it takes a while for the program to be executed with such big numbers.

    • @walex1281
      @walex1281 2 ปีที่แล้ว

      It's datatype will be change u have to use long then instead of int

  • @mustafadurmus9431
    @mustafadurmus9431 ปีที่แล้ว

    one is not a prime number :)

  • @ghettoasfuckxp
    @ghettoasfuckxp 5 ปีที่แล้ว

    .