ProblemWe are required to write a JavaScript function that takes in a number n. Our function should find all such number x and y such that −x^2 - 4y^2 = n.And it should return an array of all such pairs.ExampleFollowing is the code − Live Democonst num = 90005; const findSolution = (num = 1) => { const res = []; let a, b; for(let a = 1; a
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should find and return that smallest possible number which is divisible by all numbers from 1 to n.ExampleFollowing is the code − Live Democonst num = 11; const smallestDivisible = (num = 1) => { let res = num * (num - 1) || 1; for (let i = num - 1; i >= 1; i--) { if (res % i) { for (let j = num - 1; j >= 1; j--) { if (!(i % j) && !(res % j)) { res = i * res / j; break; } } } } return res; } console.log(smallestDivisible(num));Output27720
When it is required to create a list of tuple, and have the first element as the number, and the second element as the square of the element, list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_list = [23, 42, 67, 89, 11, 32] print(“The list is “) print(my_list) print(“The resultant tuple is :”) my_result = [(val, pow(val, 2)) for val in my_list] print(my_result)OutputThe list is [23, 42, 67, 89, 11, 32] The resultant tuple is : [(23, 529), (42, 1764), (67, 4489), (89, 7921), (11, 121), (32, 1024)]ExplanationA list is defined, and is displayed on ... Read More
When it is required to find the second largest number in a list using bubble sort, a method named ‘bubble_sort’ is defined, that sorts the elements of the list. Once this is done, another method named ‘get_second_largest’ is defined that returns the second element from the end as output.Below is the demonstration of the same −Example Live Demomy_list = [] my_input = int(input("Enter the number of elements...")) for i in range(1, my_input+1): b=int(input("Enter the element...")) my_list.append(b) for i in range(0, len(my_list)): for j in range(0, len(my_list)-i-1): if(my_list[j]>my_list[j+1]): temp=my_list[j] ... Read More
When it is required to merge two lists and sort them, a method can be defined that sorts the list using ‘sort’ method.Below is the demonstration of the same −Example Live Demodef merge_list(list_1, list_2): merged_list = list_1 + list_2 merged_list.sort() return(merged_list) list_1 = [20, 18, 9, 51, 48, 31] list_2 = [28, 33, 3, 22, 15, 20] print("The first list is :") print(list_1) print("The second list is :") print(list_2) print(merge_list(list_1, list_2))OutputThe first list is : [20, 18, 9, 51, 48, 31] The second list is : [28, 33, 3, 22, 15, 20] [3, 9, 15, 18, 20, ... Read More
When it is required to find the gravitational force that acts between the two objects, a method named ‘find_gravity’ is used, and three parameters are passed to it.Below is the demonstration of the same −Example Live Demodef find_gravity(m_1, m_2, r): G_val = 6.673*(10**-11) F_val = (G_val*m_1*m_2)/(r**2) return round(F_val, 2) m_1 = 6000000 m_2 = 1000000 r = 45 print("The gravitational force is: ") print(find_gravity(m_1, m_2, r), "N")OutputThe gravitational force is: 0.2 NExplanationA method named ‘find_gravity’ is defined, that takes three parameters.The gravitational force, and the gravitational constant are determined, and the force value is returned as ... Read More
ProblemWe are required to write a JavaScript function that takes in two sorted arrays of numbers our function should merge all the elements of both the arrays into a new array and return that new array sorted in the same order.ExampleFollowing is the code − Live Democonst arr1 = [1, 3, 4, 5, 6, 8]; const arr2 = [4, 6, 8, 9, 11]; const mergeSortedArrays = (arr1 = [], arr2 = []) => { const res = []; let i = 0; let j = 0; while(i < arr1.length && j < arr2.length){ if(arr1[i] ... Read More
ProblemWe are required to write a JavaScript function that takes in an array containing a sequence of integers, each element of which contains a possible value ranging between -1 and 1.Our function should return the size of the longest sub-section of that sequence with a sum of zero or higher.ExampleFollowing is the code − Live Democonst arr = [-1, -1, 0, 1, 1, -1, -1, -1]; const longestPositiveSum = (arr = []) => { let sum = 0; let maxslice = 0; let length = arr.length; const sumindex = []; let marker = length * 2 ... Read More
ProblemWe are required to write a JavaScript function that takes in a number and return true if the reverse of that number is a prime number, false otherwise.ExampleFollowing is the code − Live Democonst num = 13; const findReverse = (num) => { return +num .toString() .split('') .reverse() .join(''); }; const isPrime = (num) => { let sqrtnum = Math.floor(Math.sqrt(num)); let prime = num !== 1; for(let i = 2; i < sqrtnum + 1; i++){ if(num % i === 0){ prime = false; break; }; }; return prime; } const isReversePrime = num => isPrime(findReverse(num)); console.log(isReversePrime(num));Outputtrue
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return the next higher multiple of five of that number, obtained by concatenating the shortest possible binary string to the end of this number's binary representation.ExampleFollowing is the code −const generateAll = (num = 1) => { const res = []; let max = parseInt("1".repeat(num), 2); for(let i = 0; i { const numBinary = num.toString(2); let i = 1; while(true){ const perm = generateAll(i); const required = perm.find(binary => { return parseInt(numBinary + binary, 2) % 5 === 0; }); if(required){ return parseInt(numBinary + required, 2); }; i++; }; }; console.log(smallestMultiple(8));Output35