importjava.util.Scanner;publicclassPerfectNumber{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.print("请输入一个正整数N:");intN=scanner.nextInt();scanner.close();for(inti=1;i<=N;i++){intsum=0;for(intj=1;j<i;j++){if(i%j==0){sum+=j;}}// ...
当然,你要是把int范围内的5个Perfect Number(第5个是33550336)都找出来了,直接做判断也行。 publicbooleancheckPerfectNumber3(intnum){int[] primes = {2,3,5,7,13};for(intp: primes) {if((1<< (p -1)) * ((1<< p) -1) == num) {returntrue; } }returnfalse; } 05 小结 算法专题目前...
Enterany number:1919isaPrimeNumber Output 2: Enterany number:66isnotaPrimeNumber You can also usewhile loopto check the prime number: Just replace this part of the code in above program: for(inti=2;i<=num/2;i++){temp=num%i;if(temp==0){isPrime=false;break;}} with this: inti=2;w...
AC Java: 1publicclassSolution {2publicbooleancheckPerfectNumber(intnum) {3if(num == 1){4returnfalse;5}67intsum = 1;8for(inti = 2; i<Math.sqrt(num); i++){9if(num%i == 0){10sum += i+num/i;11}12}13returnsum ==num;14}15}...
Java program to check if number is power of two: In this tutorial, we will see how to check if number is power of two. There are many approaches to check if number is power of two or not. Approach 1: It is very easy and straight forward approach. Run a while loop which checks fo...
Leetcode#ProblemLevelTagsTimeSpaceLanguageSequence N/A Jump Game II.java Hard [Array, Coordinate DP, DP, Greedy] O(n) O(1) Java 0 N/A Majority Number II.java Medium [Enumeration, Greedy] Java 1 N/A Search a 2D Matrix II.java Medium [Binary Search, Divide and Conquer] Java 2 N/A...
6 Fraction to Recurring Decimal.java Medium Java [Hash Table, Math] 7 Gray Code.java Medium Java [Backtracking] 8 Group Shifted Strings.java Easy Java [] 9 H-Index.java Medium Java [Hash Table, Sort] 10 Hamming Distance.java Easy Java [] 11 Happy Number.java Easy Java [] 12...
particular algorithms are specified for the classRandom. Java implementations must use all the algorithms shown here for the classRandom, for the sake of absolute portability of Java code. However, subclasses of classRandomare permitted to use other algorithms, so long as they adhere to the genera...
A. The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep(...
Once a new test code is added, all test scenarios should be re-executed to ensure everything is fine. Advantages JUnit offers test runners to execute tests. Offers assertions to test expected outcomes. With the latest version of JUnit 5, exceptions are identified quickly. You can even execute...