Algorithm to find out whether an input is a prime number or not

You can start by researching what defines a prime number and how to check for it. Then, you can look into the Newton method and its application in computing square roots. Finally, investigate what makes a number perfect and how to determine if a given input fits that criteria. Once you have a good understanding of these concepts, you can begin creating algorithms and programs to solve the given tasks.
  • #1
hadi amiri 4
98
1

Homework Statement



1- write an algorithm to find out whether an input is a prime number
or not.
2-write aa program to compute square root by Newton method
3-write an algorithm to show an input is perfect or not.
4-

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2


hadi amiri 4 said:

Homework Statement



1- write an algorithm to find out whether an input is a prime number
or not.
2-write aa program to compute square root by Newton method
3-write an algorithm to show an input is perfect or not.
4-

Homework Equations





The Attempt at a Solution

Before getting any help on this forum, you need to show what you have tried to do.
 
  • #3


1. Algorithm to determine if a number is prime:
Step 1: Start
Step 2: Take input from the user and store it in a variable 'num'
Step 3: Initialize a variable 'flag' to 0
Step 4: If 'num' is less than 2, then print "Not a prime number" and go to Step 8
Step 5: Use a for loop to iterate from 2 to 'num'-1
Step 6: Inside the loop, check if 'num' is divisible by the current value of the loop variable. If yes, set 'flag' to 1 and break out of the loop.
Step 7: After the loop, check the value of 'flag'. If it is 1, then print "Not a prime number", else print "Prime number".
Step 8: Stop.

2. Program to compute square root using Newton's method:
Step 1: Start
Step 2: Take input from the user and store it in a variable 'num'
Step 3: Initialize a variable 'guess' to 'num'/2
Step 4: Initialize a variable 'diff' to 1
Step 5: Use a while loop to iterate until 'diff' is less than or equal to 0.0001
Step 6: Inside the loop, calculate the next guess using the formula: guess = (guess + 'num'/guess)/2
Step 7: Update 'diff' to the difference between 'num' and the square of 'guess'
Step 8: After the loop, print the final value of 'guess' as the square root of 'num'
Step 9: Stop.

3. Algorithm to determine if a number is perfect:
Step 1: Start
Step 2: Take input from the user and store it in a variable 'num'
Step 3: Initialize a variable 'sum' to 0
Step 4: Use a for loop to iterate from 1 to 'num'-1
Step 5: Inside the loop, check if 'num' is divisible by the current value of the loop variable. If yes, add the value to 'sum'.
Step 6: After the loop, check if 'sum' is equal to 'num'. If yes, then print "Perfect number", else print "Not a perfect number".
Step 7: Stop.
 

1. How does the algorithm determine if a number is prime or not?

The algorithm checks if the input number is divisible by any number other than 1 and itself. If it is not divisible by any other number, then it is considered a prime number.

2. What is the time complexity of this algorithm?

The time complexity of this algorithm is O(sqrt(n)), where n is the input number. This means that the algorithm's runtime increases proportionally to the square root of the input number.

3. Can this algorithm be used for large numbers?

Yes, this algorithm can be used for large numbers. However, as the input number increases, the runtime of the algorithm also increases, making it less efficient for very large numbers.

4. Are there any limitations to this algorithm?

One limitation of this algorithm is that it can only determine if a number is prime or not. It does not provide any information on the factors of the number.

5. Are there any alternative algorithms for finding prime numbers?

Yes, there are many other algorithms for finding prime numbers, such as the Sieve of Eratosthenes and the Miller-Rabin primality test. Each algorithm has its own advantages and disadvantages, and the best one to use depends on the specific situation.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
33
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Replies
5
Views
415
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Programming and Computer Science
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
911
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top