0% found this document useful (0 votes)
8 views

Algorithm to Check if a Number is Prime

Algorithm to Check if a Number is Prime

Uploaded by

iammayank965
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Algorithm to Check if a Number is Prime

Algorithm to Check if a Number is Prime

Uploaded by

iammayank965
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Algorithm to Check if a Number is Prime

1. Input: A number n.
2. Output: Whether n is a prime number or not.

Steps

1. Check if n is less than or equal to 1:


o If n≤1n \leq 1n≤1, then n is not a prime number. End the algorithm.
2. Check if n is equal to 2:
o If n=2n = 2n=2, then n is a prime number. End the algorithm.
3. Check divisibility by numbers from 2 to the square root of n:
o For iii from 2 to n\sqrt{n}n:
▪ If n is divisible by iii (i.e., n%i==0n \% i == 0n%i==0):
▪ n is not a prime number. End the algorithm.
o If no divisors are found, n is a prime number.
4. Conclusion:
o If no divisors were found in step 3, n is a prime number.
Algorithm to check if a number is gold batch

A Goldbach number is a positive even integer greater than 2 that can be expressed as the sum
of two prime numbers. The Goldbach conjecture asserts that every even integer greater than 2
can be expressed as the sum of two primes, although this has not been proven for all even
integers.

To check whether a given even number n is a Goldbach number, we can follow these steps:

Algorithm to Check if a Number is a Goldbach Number

1. Input: A number n.
2. Output: Whether n is a Goldbach number or not.

Steps

1. Check if n is an even number greater than 2:


o If n is not even or n≤2n \leq 2n≤2, return False.
2. Generate a list of prime numbers up to n:
o Use the Sieve of Eratosthenes or another method to generate all prime
numbers less than n.
3. Check pairs of primes that sum to n:
o For each prime number p in the list of primes:
▪ Calculate n−pn - pn−p.
▪ Check if n−pn - pn−p is also a prime.
▪ If such a pair is found, return True.
o If no such pair is found, return False
Algorithm to Check if a Date is a Future Date Relative to 2019

1. Input: A date d in the format YYYY-MM-DD.


2. Output: Whether d is a future date relative to January 1, 2019.

Steps

1. Parse the input date d:


o Extract the year, month, and day from the input date.
2. Define the reference date:
o The reference date is January 1, 2019.
3. Compare the input date d with the reference date:
o If the input date d is greater than the reference date, return True.
o Otherwise, return False.
Algorithm to Check if a Number is a Circular Prime

1. Check if n is a prime number:


o If n is not prime, return False.

2.Generate all rotations of n:


o Convert n to a string to easily manipulate its digits.
o Generate all rotations of the string representation of n.
3. Check if all rotations are prime:
o For each rotation, convert it back to an integer and check if it is prime.
o If any rotation is not prime, return False.
4. If all rotations are prime, return True.

1.
Algorithm to check if a number is k -boat number

1. Convert the number to a string to handle splitting.


2. Iterate through the string, splitting it into parts of length k.
3. Check if each part is divisible by k.
4. If all parts are divisible by k, the number is a k-boat number.
algorithm to check if a number is a composite magic number:

1. Check if the number is composite.


2. Calculate the sum of the digits of the number, repeatedly summing the digits until a
single digit is obtained.
3. Check if the resulting single digit is 1.

:
Algorithm to check if a number is smith number

• Check if the number is composite: A Smith number must be a composite number.


• Find the prime factors of the number: Break the number down into its prime factors.
• Sum the digits of the number: Calculate the sum of the digits of the original number.
• Sum the digits of the prime factors: Calculate the sum of the digits of the prime factors.
• Compare the sums: If the sum of the digits of the number equals the sum of the digits of
the prime factors, it is a Smith number.
Algorithm to input two valid dates, each compromising of day,month and year and calculate
the days elapsed between both dates•

is_leap_year(year): Checks if a year is a leap year.


• days_in_month(month, year): Returns the number of days in a given month of a given
year.
• days_elapsed(day1, month1, year1, day2, month2, year2): Calculates the number of
days elapsed between two dates. It calculates the total days from the start of each year to the
given date using the total_days function, then calculates the difference between the two
dates' total days to get the elapsed days.

You might also like