
Corey G. answered 07/23/20
PhD in Computer Engineering with 20 Years of Experience
These these two links should help.
https://www.google.com/amp/s/www.jquery-az.com/python-exponent/amp/
https://www.tutorialspoint.com/python/python_functions.htm
Justin S.
asked 07/17/20Corey G. answered 07/23/20
PhD in Computer Engineering with 20 Years of Experience
These these two links should help.
https://www.google.com/amp/s/www.jquery-az.com/python-exponent/amp/
https://www.tutorialspoint.com/python/python_functions.htm
See below for a python solution. We can use set data type of python to achieve O(n) complexity solution. This solution mainly focus on the algorithm part and does not implement the part that ask user to input the number. Hope the find_multiples function can save you some time.
def find_multiples(input, numbers):
abs_numbers = [abs(number) for number in numbers]
max_val, min_val = max(abs_numbers), min(abs_numbers)
numbers_set = set(numbers)
input = abs(input) # the number input by the user
if input == 0:
if 0 in numbers_set:
return [0]
else:
return []
val = input
#print (val, max_val, min_val)
results = []
while val <= max_val:
if val in numbers_set:
results.append(val)
val = val + input
return results
def main():
for input in [0, 1, 3, 8, 7, -5, -2, -1, -3, 5]:
results = find_multiples(input, [5, 13, 6, 21, 17, 51, 10, 33])
print("Input is ", input, ", multiples are ", results)
if __name__ == "__main__":
main()
Azmain F. answered 07/19/20
Competitive Problem Solver Specializing in Python
In Python:
Patrick B. answered 07/17/20
Math and computer tutor/teacher
/*****************************************************
Justin: here are the source codes for the FOUR (4)
problems that you have posted....
Sorry, no Python... just java
***************************************************/
import java.io.*;
import java.lang.Math.*;
class Justin
{
private int N;
private int X;
private int Y;
private int Z;
public double CoinProbability()
{
N = PromptForInteger("Please input the # N :>",1,32767);
double prob = 1.0f/ Math.pow(2,N);
System.out.println(" Probability of " + N + " in a row = " + prob);
return(prob);
}
public int GetN() { return(N); }
public int GetX() { return(X); }
public int GetY() { return(Y); }
public int GetZ() { return(Z); }
public void CompareSum()
{
X = PromptForInteger(" Please input the 1st # :>",-15000,15000);
Y = PromptForInteger(" Please input the 2nd # :>",-15000,15000);
Z = PromptForInteger(" Please input the 3rd # :>",-32767,32767);
if ((X+Y)<Z)
{
System.out.println("The total of " + X + "+" + Y + " is LESS Than " + Z);
}
else if ((X+Y)>Z)
{
System.out.println("The total of " + X + "+" + Y + " is GREATER Than " + Z);
}
else
{
System.out.println("The total of " + X + "+" + Y + " is EQUAL to " + Z);
}
}
public int PromptForInteger(String promptMsg, int iMin, int iMax)
{
String inbuff;
Console console;
int iReturn=-1;
do
{
console = System.console();
System.out.print(promptMsg);
inbuff = console.readLine();
iReturn = Integer.parseInt(inbuff);
} while ((iReturn<iMin) || (iReturn>iMax));
return(iReturn);
} //PromptForInteger
public void GradeAverage( int [][] A)
{
double classTotal = 0;
double classAvg = 0;
int N = A.length;
if (N==0) { return;}
int iLoop, jLoop;
for (iLoop=0; iLoop<N; iLoop++)
{
double rowSum = 0;
int rowCount = A[iLoop].length;
for (jLoop=0; jLoop<rowCount; jLoop++)
{
rowSum += A[iLoop][jLoop];
System.out.print(A[iLoop][jLoop] + " ");
}
double rowAvg = rowSum / rowCount;
System.out.println(" row Avg = " + rowAvg);
classTotal += rowAvg;
}
classAvg = classTotal/N;
System.out.println(" Class average = " + classAvg);
}
private int LinearSearch( int [] A, int iTarget)
{
int iReturn=-1;
for (int iLoop=0; iLoop<A.length; iLoop++)
{
if (iTarget==A[iLoop])
{
iReturn = iLoop;
break;
}
}
return(iReturn);
}
private int Max(int [] A)
{
int iMax = -32767;
for (int iLoop=0; iLoop<A.length; iLoop++)
{
if (A[iLoop]>iMax)
{
iMax = A[iLoop];
}
}
return(iMax);
}
public void ShowMultiples()
{
int [] A = {5,13,6,21,17,51,10,33};
int t = PromptForInteger("Please enter the # :>",1,10000);
int N=1;
int n=-32767;
while (n<=Max(A))
{
n=N*t;
if (LinearSearch(A,n)>=0)
{
System.out.println(n);
}
N++;
}
}
public static void main(String args[])
{
Justin x = new Justin();
x.CoinProbability();
x.CompareSum();
int A[][] = {{95,92,86,87},{65,54},{89,72,100},{33,0,0,70}};
x.GradeAverage(A);
x.ShowMultiples();
} //main
} //class
Patrick B. answered 07/17/20
Math and computer tutor/teacher
/*****************************************************
Justin: here are the source codes for the FOUR (4)
problems that you have posted....
Sorry, no Python... just java
***************************************************/
import java.io.*;
import java.lang.Math.*;
class Justin
{
private int N;
private int X;
private int Y;
private int Z;
public double CoinProbability()
{
N = PromptForInteger("Please input the # N :>",1,32767);
double prob = 1.0f/ Math.pow(2,N);
System.out.println(" Probability of " + N + " in a row = " + prob);
return(prob);
}
public int GetN() { return(N); }
public int GetX() { return(X); }
public int GetY() { return(Y); }
public int GetZ() { return(Z); }
public void CompareSum()
{
X = PromptForInteger(" Please input the 1st # :>",-15000,15000);
Y = PromptForInteger(" Please input the 2nd # :>",-15000,15000);
Z = PromptForInteger(" Please input the 3rd # :>",-32767,32767);
if ((X+Y)<Z)
{
System.out.println("The total of " + X + "+" + Y + " is LESS Than " + Z);
}
else if ((X+Y)>Z)
{
System.out.println("The total of " + X + "+" + Y + " is GREATER Than " + Z);
}
else
{
System.out.println("The total of " + X + "+" + Y + " is EQUAL to " + Z);
}
}
public int PromptForInteger(String promptMsg, int iMin, int iMax)
{
String inbuff;
Console console;
int iReturn=-1;
do
{
console = System.console();
System.out.print(promptMsg);
inbuff = console.readLine();
iReturn = Integer.parseInt(inbuff);
} while ((iReturn<iMin) || (iReturn>iMax));
return(iReturn);
} //PromptForInteger
public void GradeAverage( int [][] A)
{
double classTotal = 0;
double classAvg = 0;
int N = A.length;
if (N==0) { return;}
int iLoop, jLoop;
for (iLoop=0; iLoop<N; iLoop++)
{
double rowSum = 0;
int rowCount = A[iLoop].length;
for (jLoop=0; jLoop<rowCount; jLoop++)
{
rowSum += A[iLoop][jLoop];
System.out.print(A[iLoop][jLoop] + " ");
}
double rowAvg = rowSum / rowCount;
System.out.println(" row Avg = " + rowAvg);
classTotal += rowAvg;
}
classAvg = classTotal/N;
System.out.println(" Class average = " + classAvg);
}
private int LinearSearch( int [] A, int iTarget)
{
int iReturn=-1;
for (int iLoop=0; iLoop<A.length; iLoop++)
{
if (iTarget==A[iLoop])
{
iReturn = iLoop;
break;
}
}
return(iReturn);
}
private int Max(int [] A)
{
int iMax = -32767;
for (int iLoop=0; iLoop<A.length; iLoop++)
{
if (A[iLoop]>iMax)
{
iMax = A[iLoop];
}
}
return(iMax);
}
public void ShowMultiples()
{
int [] A = {5,13,6,21,17,51,10,33};
int t = PromptForInteger("Please enter the # :>",1,10000);
int N=1;
int n=-32767;
while (n<=Max(A))
{
n=N*t;
if (LinearSearch(A,n)>=0)
{
System.out.println(n);
}
N++;
}
}
public static void main(String args[])
{
Justin x = new Justin();
x.CoinProbability();
x.CompareSum();
int A[][] = {{95,92,86,87},{65,54},{89,72,100},{33,0,0,70}};
x.GradeAverage(A);
x.ShowMultiples();
} //main
} //class
Patrick B. answered 07/17/20
Math and computer tutor/teacher
/*****************************************************
Justin: here are the source codes for the FOUR (4)
problems that you have posted....
Sorry, no Python... just java
***************************************************/
import java.io.*;
import java.lang.Math.*;
class Justin
{
private int N;
private int X;
private int Y;
private int Z;
public double CoinProbability()
{
N = PromptForInteger("Please input the # N :>",1,32767);
double prob = 1.0f/ Math.pow(2,N);
System.out.println(" Probability of " + N + " in a row = " + prob);
return(prob);
}
public int GetN() { return(N); }
public int GetX() { return(X); }
public int GetY() { return(Y); }
public int GetZ() { return(Z); }
public void CompareSum()
{
X = PromptForInteger(" Please input the 1st # :>",-15000,15000);
Y = PromptForInteger(" Please input the 2nd # :>",-15000,15000);
Z = PromptForInteger(" Please input the 3rd # :>",-32767,32767);
if ((X+Y)<Z)
{
System.out.println("The total of " + X + "+" + Y + " is LESS Than " + Z);
}
else if ((X+Y)>Z)
{
System.out.println("The total of " + X + "+" + Y + " is GREATER Than " + Z);
}
else
{
System.out.println("The total of " + X + "+" + Y + " is EQUAL to " + Z);
}
}
public int PromptForInteger(String promptMsg, int iMin, int iMax)
{
String inbuff;
Console console;
int iReturn=-1;
do
{
console = System.console();
System.out.print(promptMsg);
inbuff = console.readLine();
iReturn = Integer.parseInt(inbuff);
} while ((iReturn<iMin) || (iReturn>iMax));
return(iReturn);
} //PromptForInteger
public void GradeAverage( int [][] A)
{
double classTotal = 0;
double classAvg = 0;
int N = A.length;
if (N==0) { return;}
int iLoop, jLoop;
for (iLoop=0; iLoop<N; iLoop++)
{
double rowSum = 0;
int rowCount = A[iLoop].length;
for (jLoop=0; jLoop<rowCount; jLoop++)
{
rowSum += A[iLoop][jLoop];
System.out.print(A[iLoop][jLoop] + " ");
}
double rowAvg = rowSum / rowCount;
System.out.println(" row Avg = " + rowAvg);
classTotal += rowAvg;
}
classAvg = classTotal/N;
System.out.println(" Class average = " + classAvg);
}
private int LinearSearch( int [] A, int iTarget)
{
int iReturn=-1;
for (int iLoop=0; iLoop<A.length; iLoop++)
{
if (iTarget==A[iLoop])
{
iReturn = iLoop;
break;
}
}
return(iReturn);
}
private int Max(int [] A)
{
int iMax = -32767;
for (int iLoop=0; iLoop<A.length; iLoop++)
{
if (A[iLoop]>iMax)
{
iMax = A[iLoop];
}
}
return(iMax);
}
public void ShowMultiples()
{
int [] A = {5,13,6,21,17,51,10,33};
int t = PromptForInteger("Please enter the # :>",1,10000);
int N=1;
int n=-32767;
while (n<=Max(A))
{
n=N*t;
if (LinearSearch(A,n)>=0)
{
System.out.println(n);
}
N++;
}
}
public static void main(String args[])
{
Justin x = new Justin();
x.CoinProbability();
x.CompareSum();
int A[][] = {{95,92,86,87},{65,54},{89,72,100},{33,0,0,70}};
x.GradeAverage(A);
x.ShowMultiples();
} //main
} //class
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.