Skip to content

Instantly share code, notes, and snippets.

View bijay-shrestha's full-sized avatar
👨‍🎓
MSCS Compro

Bijay Shrestha bijay-shrestha

👨‍🎓
MSCS Compro
  • Bank of America
  • SF, CA
View GitHub Profile
package com.hawa.practice;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
@Slf4j
public class PrimeFactors {
public static void main(String[] args) {
/**
* The fundamental theorem of arithmetic states that every natural number greater than 1 can be written as a unique product of prime
* numbers. So, for instance, 6936=2*2*2*3*17*17. Write a method named encodeNumber what will encode a number n as an array that
* contains the prime numbers that, when multipled together, will equal n. So encodeNumber(6936) would return the array {2, 2, 2, 3,
* 17, 17}. If the number is <= 1 the function should return null;
*
* Hint: proceed as follows:
* 1. Compute the total number of prime factors including duplicates.
* 2. Allocate an array to hold the prime factors. Do not hard-code the size of the returned array!!
* 3. Populate the allocated array with the prime factors. The elements of the array when multiplied together should equal the number.
/**
* Define an array to be a railroad-tie array if the following three conditions hold
* a. The array contains at least one non-zero element
* b. Every non-zero element has exactly one non-zero neighbor
* c. Every zero element has two non-zero neighbors.
*
*
* For example, {1, 2, 0, 3, -18, 0, 2, 2} is a railroad-tie array because
* a[0] = 1 has exactly one non-zero neighbor (a[1])
* a[1] = 2 has exactly one non-zero neighbor (a[0])
/**
* Calculate an array of 5 floats and calculate their sum.
*/
package com.hawa.practice;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class PracticeSetOne {
/**
* Write a program to find out whether a given integer is present in an array or not.
**/
package com.hawa.practice;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class PracticeSetTwo {
/**
* The number 198 has the property that 198 = 11 + 99 + 88, i.e., if each of its digits is concatenated twice and then summed, the result
* will be the original number. It turns out that 198 is the only number with this property.
* <p>
* However, the property can be generalized so that each digit is concatenated n times and then summed.
* For example, 2997 = 222+999+999+777 and here each digit is concatenated
* three times.
* <p>
* Write a function named checkContenatedSum that tests if a number has this generalized property.
* <p>
/**
* Define an m-n sequenced array to be an array that contains one or more occurrences of all the integers between m and n inclusive.
*
* Furthermore, the array must be in ascending order and contain only those integers. For example, {2, 2, 3, 4, 4, 4, 5} is a 2-5 sequenced
* array.
*
* The array {2, 2, 3, 5, 5, 5} is not a 2-5 sequenced array because it is missing a 4. The array {0, 2, 2, 3, 3} is not a 2-3 sequenced
* array because the 0 is out of range. And {1,1, 3, 2, 2, 4} is not a 1-4 sequenced array because it is not in ascending order.
*
* Write a method named isSequencedArray that returns 1 if its argument is a m-n sequenced array, otherwise it returns 0.
package com.hawa.practice;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class FactorsOfNumber {
public static void main(String[] args) {
int num = 10;
log.info("The factors of {} are {}", num, getFactorsOfNumber(num));
}
/**
* Define the n-based integer rounding of an integer k to be the nearest multiple of n to k. If two multiples of n are equidistant use the greater one.
*
* For example
* the 4-based rounding of 5 is 4 because 5 is closer to 4 than it is to 8,
* the 5-based rounding of 5 is 5 because 5 is closer to 5 that it is to 10,
* the 4-based rounding of 6 is 8 because 6 is equidistant from 4 and 8, so the greater one is used,
* the 13-based rounding of 9 is 13, because 9 is closer to 13 than it is to 0,
* Write a function named doIntegerBasedRounding that takes an integer array and rounds all its positive elements using n-based
* integer rounding.
/**
* A number is called digit-increasing if it is equal to n + nn + nnn + ... for some digit n between 1 and 9. For example 24 is digit-
* increasing because it equals 2 + 22 (here n = 2)
*
* Write a function called isDigitIncreasing that returns 1 if its argument is digit-increasing otherwise, it returns 0.
*
* if n is then function returns reason
* 7 1 because 7 = 7 (here n is 7)
* 36 1 because 36 = 3 + 33
* 984 1 because 984 = 8 + 88 + 888