Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If the sample code is needed, I can provide that a well. Method 1: public static

ID: 3751357 • Letter: I

Question

If the sample code is needed, I can provide that a well.

Method 1:
public static boolean isPrime(int i)

This method should return true if and only if the input i is a positive prime number. A prime is a number which is only divisible by 1 and itself (however the numbers 0 and 1 are not considered primes).

Method 2:
public static int numPrimes(int lower, int upper)

This method will return the total number of primes in the range between lower and upper, including lower and upper themselves.

Hint: it's possible to use isPrime(int i) as part of the computation.

Method 3:
public static int factorial(int n)

Find the factorial of n, which is given by n! = 1*2*...*(n-1)*n. The factorial of zero is a special case which is equal to 1.

Method 4:
public static double sumPower(int n, double p)

Sums the series 1p+2p+...+(n-1)p+np.

Method 5:
public static int boundedSum(int[] list, int low, int high)

Sums up the elements in list, but only including the elements x in list for which low x high.

Method 6:
public static double filteredSum(double[] list, double[] filterList)

Assume that list and filterList are lists of the same length. Then if the elements in list are l0, l1, ..., ln-2, ln-1 and the elements in filterList are f0, f1, ..., fn-2, fn-1, then this method will returnl0f0+l1f1+...+ln-2fn-2+ln-1fn-1.

Method 7:
public static double geometricMean(double[] list)

Computes the geometric mean of the numbers in list. If the n numbers in list are l0, l1, ..., ln-2, ln-1, then the geometric mean is defined as the nth root of the product l0l1...ln-2ln-1. You may assume that the list is not empty and the numbers are all positive.

Hint: you may use the built-in Math.pow().

Method 8:
public static int largestConsecutiveDiff(int[] list)

Considering the set of differences between consecutive elements in list, this method returns the largest. Consecutive difference is defined as the latter element minus the earlier element, i.e. l1-l0. If there are not enough elements in the list to compute any differences, then the method should return zero by default.

Method 9:
public static int largestElementWithDups(int[] list)

A duplicate element is an element which appears more than once in the list. This method returns the largest element in list which has any duplicates whatsoever. If none of the elements have duplicates (every element in the list is unique) then this method should return Integer.MIN_VALUE.

Method 10:
public static void findElementFreqs(int[] list, int[] freqs)

Assume that list and freqs are lists of the same size. For each element in list, this method should count the number of times that elements occurs in the list, and store the value in the same spot in freqs.

For example, if list[3] == 4, and the number 4 appears a total of 7 times in list, then the value 7 should be stored in freqs[3]. The same goes for all elements in freqs

Most of the code isn't giving me difficulties, but I am coming into issues with answer Methods 8, 9, 10. I looked up help here and it is limited, and my Professor is not allowing the use of hashmaps. I don't know how to approach this.

Explanation / Answer

Method 1:

public static boolean isPrime(int i)

{

boolean flag = false;

for(int j = 2; j <= i/2; ++j)

{

// condition for nonprime number

if( i% j == 0)

{

flag = true;

break;

}

}

if (!flag)

System.out.println( "True.");

else

System.out.println(num + "False");

}

Method 2:

public class Prime {

public static void main(String[] args) {

int low = 20, high = 50;

while (low < high) {

boolean flag = false;

for(int i = 2; i <= low/2; ++i) {

// condition for nonprime number

if(low % i == 0) {

flag = true;

break;

}

}

if (!flag)

System.out.print(low + " ");

++low;

}

}

}

Method 3:

class Factorial{

static int factorial(int n){    

  if (n == 0)    

    return 1;    

  else    

    return(n * factorial(n-1));    

}    

public static void main(String args[]){

  int i,fact=1;

  int number=4;//It is the number to calculate factorial    

  fact = factorial(number);   

  System.out.println("Factorial of "+number+" is: "+fact);    

}

}  

Method 4:

class series {

          

    // Function to find the sum of series

    static int seriesSum(int n,double p)

    {

        int sum = 0;

        for (int i = 1; i <= n; i++)

        sum += (i * (i + 1)/2)*p ;

        return sum;

    }

  

    // Driver code

    public static void main (String[] args)

    {

        int n = 4;

        System.out.println(seriesSum(n));

          

    }

}

method 5:

This for sum of element in a list

void printPairs(int arr[], int arr_size, int sum)

{

    unordered_set<int> s;

    for (int i = 0; i < arr_size; i++)

    {

        int temp = sum - arr[i];

        if (temp >= 0 && s.find(temp) != s.end())

            cout << "Pair with given sum " << sum <<

                 " is (" << arr[i] << ", " << temp <<

                 ")" << endl;

        s.insert(arr[i]);

    }

}

Method for largest consecutive difference:

/ /Return the maximum Sum of difference between

// consecutive elements.

int maxConsecutiveDifference(int arr[], int n)

{

    int sum = 0;

    // Sorting the array.

    sort(arr, arr + n);

    // Subtracting a1, a2, a3,....., a(n/2)-1, an/2

    // twice and adding a(n/2)+1, a(n/2)+2, a(n/2)+3,.

    // ...., an - 1, an twice.

    for (int i = 0; i < n/2; i++)

    {

        sum -= (2 * arr[i]);

        sum += (2 * arr[n - i - 1]);

    }

    return sum;

}

Method for Largest Element with Dups:

void largestelementwithdups(int arr[], int n) //array and array length

{

    // Store elements and their counts in

    // hash table

    unordered_map<int, int> mp;

    for (int i = 0; i < n; i++)

        mp[arr[i]]++;

    // Since we want elements in same order,

    // we traverse array again and print

    // those elements that appear more than

    // once.

    for (int i = 0; i < n; i++)

    {

      if (mp[arr[i]] > 1)

      {

          cout << arr[i] << " ";

          // This is tricky, this is done

          // to make sure that the current

          // element is not printed again

          mp[arr[i]] = 0;

      }

    }

}

Method for frequency by example :

#include<iostream>

using namespace std;

int Elementfrequency(int a[], int n, int x)

{

    int count = 0;

    for (int i=0; i < n; i++)

       if (a[i] == x)

          count++;

    return count;

}

// Driver program

int main() {

    int a[] = {0, 5, 5, 5, 4};

    int x = 5;

    int n = sizeof(a)/sizeof(a[0]);

    cout << Elementfrequency(a, n, x);

    return 0;

}

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote