Concept explainers
Part 3: Factorial function
In computer science, and other fields, we frequently need to know how many possible combinations are possible given a specific number of numbers or characters. For example, consider three color cards: red (R), green (G), and blue (B). How many different orders are possible of these three cards?
RGB, RBG, GRB, GBR, BRG, BGR = six (6) possible combinations
The mathematical term for this calculation is factorial (see Factorial Function !Links to an external site. web page) and is expressed using the exclamation point (!). Surprisingly, the JavaScript Math object does not have a function for factorial, so we will write our own.
So how do we calculate factorial? The steps to accomplish a task are known as an
3! = 1 * 2 * 3 = 6
Factorials only work on values >= 0, with a special rule for 0! = 1.
Below are the factorial rules you will need to implement.
Input | Result |
Input > 0 | Calculate and return factorial |
Input = 0 | Return 1 by rule |
Input < 0 | Return -1 to indicate error |
Non-numeric input | Return -1 to indicate error |
To create a factorial function, we will give you the algorithm as comment steps, and you will provide the code.
function factorial (num) { // Step 1: Set -1 as a default result for values < 0 (invalid input) // Step 2: Test if input is > 0, calculate factorial and set result to calculated value // Step 3: Test if input = 0, set result to 1 // Return result }
>> Use the above code outline steps and rules to write the factorial function. Your function must use a for loop and if conditional syntax.
Tip: Yes, you can find solutions to this function online, but please try to solve this problem on your own. Your programming "muscles" will only improve through programming "exercise," and using online solutions is not only against the integrity policy of this course and the University, but will only make solving later problems much more difficult where no online solution exists.
Step by stepSolved in 4 steps with 2 images
- Let A = {h, i, r), B = {1,7). AxB={ BXA = { AXA = { BxB = {arrow_forwardcan you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited): # if the element at the current position is o we have reached our goal if row[pos] == o: possible = True else: # make a copy of the visited array visited = visited[:} # if the element at the current position has been already visited then it's a loop. # as then its not possible to reach the last element, set possible to False if visited[pos]: possible = False else: # set visitied for the element as True visited[pos] = True possible = False # if its possible to move left then recurse in the left direction if pos - rowlpos} > o and row_puzzle_rec(row, pos - row[pos], visited): # return immediately if the goal is reached return True # if its possible to move right then recurse in the right direction if pos + rowlpos} < len(row) and row_puzzle_rec(row, pos + row[pos], visited): # return immediately if the goal is reached. return True return possible…arrow_forward1. P VI V2 V3 V4 V5 V6 V7 V8 V9 Using the matrix P from the Floyd's II algorithm V2 9 VI 0 0 9 0 09 0: 9 8 8 8 6 0 0 8 8 8 0 9 0 V3 5 5 0 9 5 0 0 9 V4 0 0 5 0 0 2 7 V6 9 0 9 0 0 9 1 0 8 8 V5 0 1 9 0 6 8 0 0 V7 9 9 9 V8 9 9 9 9 9 7 9 0 9 0 0 9 0 6 7 V9 5308OOS 3 0 Find the path from v4 to v that has the minimum cost:arrow_forward
- 2. Fill in the blanks for the number conversions: (a) (110010101101)2=( D16 (b) (A1FE)16=( (c) (2020)10=( D16=( 2=(arrow_forwardAnswer me question 2 don't use chat get didn't work please Answer in detailarrow_forwardThe Problem A Zeckendorf number is defined for all positive integers as the number of Fibonacci numbers which must be added to equal a given number k. So, the positive integer 28 is the sum of three Fibonacci numbers (21, 5, and 2), so the Zeckendorf number for k = 28 is three. Wikipedia is a satisfactory reference for our purposes for more details of the Zeckendorf and Fibonacci numbers. In both cases, you do not need to read the entire Wikipedia entry. Your assignment is to write two ARM assembly language functions which calculate Zeckendorf and Fibonacci numbers. Zeck function The first function, which should be named zeck, receives an integer parameter in register zero. This will be the variable k we discussed above. Your code should return the Zeckendorf number for k in register zero when complete. If the parameter k is zero, return zero. If the parameter k is negative, return minus 1. If the parameter k is too large to calculate, return minus 1. Fib function The other function,…arrow_forward
- Assignment-2-162-sumer 22.000X 5.0 KB) ZOOM 5. Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a boolean method that uses recursion to determine whether a string argument is a palindrome. The method should return true if the argument reads the same forward and backward. Demonstrate the method in a program.arrow_forwardPls help. Pls generate this using MATLAB pls plsarrow_forwardTrue or false: Strong scaling is not bound by Amdahl's Law.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education