Question

Generate prime number considering a couple of facts. first one needs the factors for a number...

Generate prime number considering a couple of facts. first one needs the factors for a number to see if its prime. for example 4 has prime while 5 has factors of 1,5=6 so 5 is the prime. given these facts write the following functions using only javascript functional programming elements.

Homework Answers

Answer #1
---------------------------------------------------------------------------------------------

Javascript Code:

function findPrimeFactors (num) {

var primeFactors = [];
while (num % 2 === 0) {
primeFactors.push(2);
num = num / 2;
}
  
var sqrtNum = Math.sqrt(num);
for (var i = 3; i <= sqrtNum; i++) {
while (num % i === 0) {
primeFactors.push(i);
num = num / i;
}
}

if (num > 2) {
primeFactors.push(num);
}
return primeFactors;
}

//Output Example:

console.log(findPrimeFactors(10)); // [2, 5]
console.log(findPrimeFactors(11)); // [11]

Know the answer?
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
Similar Questions
Using R Programming language write a program to Check Prime Number (entered by the user) using...
Using R Programming language write a program to Check Prime Number (entered by the user) using control statements. Note: a prime number is an integer number greater than 1 whose only factors are 1 and itself. For example: 2, 3, 5, 7, 11, 13, 17.19 etc. USING R PROGRAMMING
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater than one whose only factors are one and itself. For example, the first ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. A twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes. For example, the five twin prime pairs are (3, 5),...
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
See four problems attached. These will ask you to think about GCDs and prime factorizations, and...
See four problems attached. These will ask you to think about GCDs and prime factorizations, and also look at the related topic of Least Common Multiples (LCMs). The prime factorization of numbers can be used to find the GCD. If we write the prime factorization of a and b as a = p a1 1 p a2 2 · p an n b = p b1 1 p b2 2 · p bn n (using all the primes pi needed...
There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of...
There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of frogs were sitting together on one block when they had a terrible quarrel. Now they want to jump away from one another so that the distance between them will be as large as possible. The distance between blocks numbered J and K, where JK, is computed as K-J+1. The frogs can only jump up, meaning that they can move from one block to another...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
Singly Linked List In Javascript - How to implement when given functions and tests to pass...
Singly Linked List In Javascript - How to implement when given functions and tests to pass ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a singly linked list that given the below function will pass the bottom four tests when ran in Node.js with the 'runTests()' command. Please help me generate the singly linked list that will work and pass the tests at the bottom. I will only use this as a reference so please add comments to explain what you...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Imagine that you are working in the HR department of your company. You come across the...
Imagine that you are working in the HR department of your company. You come across the following scenarios in which your input has been sought. 1. Fatima is the mother of a newborn. She is very dedicated to her work but she used to stay for longer hours at work before she had her baby. Now she tries to schedule her work so that she leaves around 5:00 p.m. Her immediate manager feels that Fatima is no longer dedicated or...
C++. Write a program that draws a rocket shape on the screen based on user input...
C++. Write a program that draws a rocket shape on the screen based on user input of three values, height, width and stages. The type of box generated (i.e.. a hollow or filled-in) is based on a check for odd or even values input by the user for the box height (or number of rows). Here is the general specification given user input for the height of the box..... Draw a hollow box for each stage if the value for...