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
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...
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...
Facts from Client Interview Tumutch and Lotta Bolloni have been married for four years. They both...
Facts from Client Interview Tumutch and Lotta Bolloni have been married for four years. They both work very hard at their jobs – Lotta being an accountant and Tumutch working as a construction manager. They decide to splurge and look into booking an all-inclusive vacation to the enchanting Fantasy Island, in Fiji for February 2020. The Bollonis do some research into different travel packages and get quotes from various agencies. They narrow their choices down to three possibilities: Travelcations &...
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
One way to represent a very large integer (one that won't fit into a variable of...
One way to represent a very large integer (one that won't fit into a variable of type short, int, or even long) is to use an array. The array is of type int, so each element in the array can hold an integer -- we will store just one digit of our number per array element. So if a user entered 2375, it might be stored as -------------------------- | 2 | 3 | 7 | 5 | ... | --------------------------...
1. Write 3n + 1 Sequence String Generator function (1 point) In lab5.py complete the function...
1. Write 3n + 1 Sequence String Generator function (1 point) In lab5.py complete the function sequence that takes one parameter: n, which is the initial value of a sequence of integers. The 3n + 1 sequence starts with an integer, n in this case, wherein each successive integer of the sequence is calculated based on these rules: 1. If the current value of n is odd, then the next number in the sequence is three times the current number...
This programming task will be a bit different. It will be more like what you would...
This programming task will be a bit different. It will be more like what you would receive if you were a maintenance engineer working in a corporate information systems or technology department. In other words, you will have some prebuilt source code provided for you that you must alter so it will meet the given programming specification.. Build a program from what you have been given as a starting-point. Rename the “starter code”. Do not add any modules; just, use...
7) Suppose your crystal's density is 2.312 g/cm3. Use conversion factors to convert this to mg/μl...
7) Suppose your crystal's density is 2.312 g/cm3. Use conversion factors to convert this to mg/μl Show all your work and all your units throughout. Now to the doctor's office: 8) I have come to see you, the doctor, because I was feeling a bit ill. The first thing the nurse did was look at me and see I had a large growth in my neck. “Wow!” she said. “How long has this been going on?” “For a couple of...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Scenario: Considering the calculations you have done so far, you need to attend to a number...
Scenario: Considering the calculations you have done so far, you need to attend to a number of import transactions for goods that companies in the United States expressed interest in. The first transaction is for the import of good quality wines from France, since a retail liquor trading chain customer in the United States, for who you have been doing imports over the past five years has a very large order this time. The producer in France informed you that...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT