Question
please show the code in java,thanks
5. Perfect Numbers Version 2: Write a program which computes perfect numbers according the following algorithm: for each prim
0 0
Add a comment Improve this question Transcribed image text
Answer #1

package abc;

import java.util.Scanner;
import java.lang.Math;
import java.math.BigInteger;

public class Perfect {
public static void main(String args[]) {
   Scanner in=new Scanner(System.in);
   System.out.println("Enter the number:");
BigInteger n=in.nextBigInteger();
if(prime(n.intValue())) {

double ans=Math.pow(2.0,n.doubleValue());
ans=ans-1;

double p1=Math.pow(2,(n.intValue()-1));

if(prime(ans)) {
   System.out.println("Prime number is :"+n+" , perfect number is : "+(ans*p1));
}else {
   System.out.println("No perfect number found!");
}
}else {
   System.out.println("Entered number is not prime!");
}
}
public static boolean prime(double m) {
   int count=0;
   for(double i=2;i<m;i++) {
       if(m%i==0) {
           count++;
       }
   }
   if(count==0) {
       return true;
   }
   return false;
}
}

output:

Enter the number:
3
ans = 7.0
p1 = 4.0
Prime number is :3 , perfect number is : 28.0

Add a comment
Know the answer?
Add Answer to:
please show the code in java,thanks 5. Perfect Numbers Version 2: Write a program which computes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. Write a logical function perfect Square that receives a positive integer number and checks if ...

    write the code in C please 4. Write a logical function perfect Square that receives a positive integer number and checks if it is a perfect square or not. Note: perfect square numbers are 4, 9,16,25,36 etc.... Write a main function that makes use of the perfect Square function to find and print all perfect squares between nl and n2. nl and n2 are end values of a range introduced by the user. ■ (inactive CAT EXE) Enter end values...

  • Using PHP: Write a function that: Given a numerical parameter in input, computes and prints all...

    Using PHP: Write a function that: Given a numerical parameter in input, computes and prints all the prime numbers up to that value. Example: If input parameter is 10, output is "2, 3, 5, 7" If input parameter is 100, output is "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97" Write a "tester function" that: Given a specific subset of inputs, tests the...

  • USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and...

    USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and returns a single Boolean value representing whether the given argument is prime (True) or not (False). After writing the function, test it by using a loop to print out all the prime numbers from 1-100. To check your results, the prime numbers from 1-100 are: 2, 3, 5, 7, 11. 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,...

  • (prime.cpp) A prime number is a number that cannot be formed by multiplying two smaller numbers...

    (prime.cpp) A prime number is a number that cannot be formed by multiplying two smaller numbers (not including 1). For example, 2, 3, 5 are prime numbers but 4 (2*2) and 6 (2*3) are not. Write a C++ program that receives the start point and end point from user and displays all the prime numbers in this range. The program also receives how many numbers to display per row. No need to validate. Print the results in a tabular format....

  • CODE MUST BE WRITTEN IN SWIFT programming language Write a function that takes in two positive...

    CODE MUST BE WRITTEN IN SWIFT programming language Write a function that takes in two positive integers and prints every prime number between (and including) them Sample Input: prime(from: 0, to: 100) Sample Output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

  • in java the program must contain the main method and example( the number 50 should be...

    in java the program must contain the main method and example( the number 50 should be the input) must be included in the main method to check the correctness of your programs. Create a GUI program to let user enter a positive integer n through the first window, and then your program prints out all the prime integers within the range [1, n) through the second window. Your program should contain two windows in total. Below are two example pictures:...

  • Write a C program which computes the count of the prime and perfect numbers within a...

    Write a C program which computes the count of the prime and perfect numbers within a given limit Land U, representing the lower and upper limit respectively. Prime numbers are numbers that have only 2 factors: 1 and themselves (1 is not prime). An integer number is said to be perfect number if its factors, including 1 (but not the number itself), sum to the number. Sample Inputi: 1 100 Sample Outputs: Prime: 25 Perfect: 2 Sample Input2: 101 10000...

  • The task involves writing a C++ program that determines the prime numbers between 1 and 100....

    The task involves writing a C++ program that determines the prime numbers between 1 and 100. The steps you should follow to identify the prime numbers are the following. 1. The number 1 is not a prime number, so it should be scratched. 2. Starting from the first prime number, which is 2, you scratch all the numbers that are the multiple of 2. You should not scratch out 2 itself. 3. The next number in the sequence after the...

  • Source: 2014 Nielsen 72 Prime Numbers A prime number is a number that is evenly divisible...

    Source: 2014 Nielsen 72 Prime Numbers A prime number is a number that is evenly divisible only by 1 and itself. The prime numbers less than 100 are listed below. 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 Choose one of these numbers at random. Find the probability that a. The number is odd b. The sum of the digits is odd c....

  • Write a java program to print all the prime numbers below a certain given number. A...

    Write a java program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT