Q. How to write a recursive algorithm to find the largest prime factor of a number in java?
import
java.io.*;
import
java.util.*;
class
GFG {
// function to find
largest prime factor
static
long
maxPrimeFactors(
long
n)
{
//
Initialize the maximum prime
//
factor variable with the
//
lowest one
long
maxPrime = -
1
;
//
Print the number of 2s
//
that divide n
while
(n %
2
==
0
) {
maxPrime
=
2
;
//
equivalent to n /= 2
n
>>=
1
;
}
//
n must be odd at this point,
//
thus skip the even numbers
//
and iterate only for odd
//
integers
for
(
int
i =
3
; i <= Math.sqrt(n); i +=
2
) {
while
(n % i ==
0
) {
maxPrime
= i;
n
= n / i;
}
}
//
This condition is to handle
//
the case when n is a prime
//
number greater than 2
if
(n >
2
)
maxPrime
= n;
return
maxPrime;
}
// Driver
code
public
static
void
main(String[]
args)
{
Long
n = 15l;
System.out.println(maxPrimeFactors(n));
n
= 25698751364526l;
System.out.println(maxPrimeFactors(n));
}
}
Output:
5 328513
Q. How to write a recursive algorithm to find the largest prime factor of a number...
Use java code please not C++ or Python. Thank you. Write a recursive algorithm that counts the number of nodes in a linked list. Analyze the runtime of your algorithm
Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an array that have ten integer values, user can enter the values or assigned the values to the array in the main method. The search key should be a value in the array or not in the array. JAVA!!!
IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an...
Implement a recursive algorithm (implemented in Java) that checks if a number is identical when read both forwards and backwards
can somebody help me with this exercise 5 Euclidean algorithm The largest common divisor (gcd) of two positive integers p and q can be given by the Euclid's algorithm explained in the lecture will be determined. · Write a function gcdIterative that uses the largest common divisor of p and q Calculates loop structure and returns. Use the pseudocode given in the lecture as a starting point and implement it as directly as possible into a C ++ program. Use...
write a recursive algorithm to find the sum of the first N terms of the series 1, 1/2, 1/3, ... 1/N
Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should meet the following requirements: 1. The program shall graphically prompt the user for a file. 2. The program shall read the selected file which will contain 1 integer per line. 3. The program shall sort the values it reads from the file from largest to smallest. 4. The program shall write the values to an output file from largest to smallest in the same...
Describe a non-recursive algorithm that takes a list of distinct integers a_1, a_2, ...., a_n and finds the sum of the primes in the list. Write your answer in pseudo-code or any well-known procedural language like Python, Java, C++, ..... You do not need to write a function to determine whether a number is prime. Assume it is part of your language. E.g. For the list 2, 3, 4, 5, 6, 7, your program should return 17 (because 2 +...
Write a recursive function in Python to find the sum of digits of a number. Name the function sum_of_digits. The function should use recursive algorithm (calling itself). The function should print out the sum of all the digits of a given number. For example, sum_of_digits(343) should have a output of 10. Marks will be deducted if you do not follow strictly to the instructions. [3]: N 1 def sum_of_digits(n): HNM in sum_of_digits (343) Out[3]: 10
Assuming that a 20-point DFT is computed using the prime factor algorithm, (a) Determine the input and output mapping tables. (b) If the input sequence is x[n] ={1/2, n even { 0, n odd carry out the 20-point DFT of x[n] step-by-step using the prime factor algorithm. points) Assuming that a 20-point DFT is computed using the prime factor algorithm, a) Determine the input and output mapping tables. (b) If the input sequence is nn even 0, n odd carry...