Finding Next Prime Number Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:14:52

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should that smallest number which is just greater than n and is a prime number.ExampleFollowing is the code − Live Democonst num = 101; 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 nextPrime = (num = 1) => {    while(!isPrime(++num)){    };    return num; }; console.log(nextPrime(num));Output103

Solve Maximum Subarray Problem Using Divide and Conquer in Python

AmitDiwan
Updated on 19-Apr-2021 11:13:29

403 Views

When it is required solve the maximum subarray problem using the divide and conquer method, Below is the demonstration of the same −Example Live Demodef max_crossing_sum(my_array, low, mid, high):    sum_elements = 0    sum_left_elements = -10000    for i in range(mid, low-1, -1):    sum_elements = sum_elements + my_array[i]    if (sum_elements > sum_left_elements):       sum_left_elements = sum_elements    sum_elements = 0    sum_right_elements = -1000    for i in range(mid + 1, high + 1):       sum_elements = sum_elements + my_array[i]       if (sum_elements > sum_right_elements):     ... Read More

Sort List of Tuples by Last Element in Python

AmitDiwan
Updated on 19-Apr-2021 11:12:41

877 Views

When it is required to sort a list of tuples in increasing order based on last element of every tuple, a method is defined, that iterates over the tuple and performs a simple swap to achieve the same.Below is the demonstration of the same −Example Live Demodef sort_tuple(my_tup):    my_len = len(my_tup)    for i in range(0, my_len):       for j in range(0, my_len-i-1):          if (my_tup[j][-1] > my_tup[j + 1][-1]):             temp = my_tup[j]             my_tup[j]= my_tup[j + 1]         ... Read More

Generate Random Numbers from 1 to 20 and Append to List in Python

AmitDiwan
Updated on 19-Apr-2021 11:12:22

569 Views

When it is required to generate random numbers within a given range and append them to a list, a method is defined, that generates random numbers and ‘append’s them to an empty list.Below is the demonstration of the same −Example Live Demoimport random def random_gen(beg, end, my_num):    my_result = []    for j in range(my_num):       my_result.append(random.randint(beg, end))    return my_result my_num = 19 beg = 1 end = 20 print("The number is :") print(my_num) print("The start and end values are :") print(beg, end) print("The elements are : ") print(random_gen(beg, end, my_num))OutputThe number is : 19 The start ... Read More

Cumulative Sum of a List in Python

AmitDiwan
Updated on 19-Apr-2021 11:11:58

422 Views

When it is required to find the sum of a list where the specific element is sum of first few elements, a method is defined, that takes list as parameter. It uses list comprehension to find the cumulative sum.Below is the demonstration of the same −Example Live Demodef cumulative_sum(my_list):    cumulative_list = []    my_length = len(my_list)    cumulative_list = [sum(my_list[0:x:1]) for x in range(0, my_length+1)]    return cumulative_list[1:] my_list = [10, 20, 25, 30, 40, 50] print("The list is :") print(my_list) print("The cumulative sum is :") print (cumulative_sum(my_list))OutputThe list is : [10, 20, 25, 30, 40, 50] The cumulative ... Read More

Find Perfect Squares with Digit Sum Less Than 10 in Python

AmitDiwan
Updated on 19-Apr-2021 11:11:40

1K+ Views

When it is required to find all numbers in a range where there are perfect square, and sum of digits in the number is less than 10, list comprehension is used.Below is the demonstration of the same −Example Live Demolower_limit = int(input(“Enter the lower range: “)) upper_limit = int(input(“Enter the upper range: “)) my_list = [] my_list = [x for x in range(lower_limit,upper_limit+1) if (int(x**0.5))**2==x and sum(list(map(int,str(x))))

Volume Difference of Cuboids in JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:08:58

125 Views

ProblemWe are required to write a JavaScript function that takes in two arrays, specifying the lengths, widths, and heights of two cuboids.Our function should calculate the volume of both cuboids and return their absolute difference.ExampleFollowing is the code − Live Democonst h1 = 10; const w1 = 12; const l1 = 15; const h2 = 12; const w2 = 15; const l2 = 9; const findVolumeDifference = (l1, w1, h1, l2, w2, h2) => {    const v1 = l1 * w1 * h1;    const v2 = l2 * w2 * h2;    const diff = Math.abs(v1 - v2);    return diff; }; console.log(findVolumeDifference(l1, w1, h1, l2, w2, h2));Output180

Find Length of Diagonal of a Cuboid Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:08:36

219 Views

ProblemWe are required to write a JavaScript function that takes in the length, width and height of a cuboid and return the length of its diagonal.ExampleFollowing is the code − Live Democonst height = 10; const width = 12; const length = 15; const findDiagonal = (l, w, h) => {    const ll = l * 2;    const ww = w * 2;    const hh = h * 2;    const sum = ll + ww + hh;    const diagonal = Math.sqrt(sum);    return diagonal; }; console.log(findDiagonal(length, width, height));Output8.602325267042627

Integers with Sum of Squared Divisors as Perfect Square in JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:08:20

253 Views

ProblemWe are required to write a JavaScript function that takes in a range specified by an array of two numbers m and n.Our function is supposed to find all integers between m and n (m and n integers such as 1

Find One Missing Number in a Scrambled Sequence Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:08:03

251 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers containing numbers from 1 to n.The problem is that one number from the array goes missing and the array is not sorted as well. Our function should find and return that one number missing from the array.ExampleFollowing is the code − Live Democonst arr = [4, 7, 1, 8, 9, 5, 2, 3]; const findMissing = (arr = []) => {    const sumArr = arr.reduce((acc, val) => acc + val);    const { length: len } = arr;    const sumFirst = (len + 1) * (len + 2) * .5;    const missing = sumFirst - sumArr;    return missing; }; console.log(findMissing(arr));Output6

Advertisements