I have an exercise about raising certain numbers to a given power.
The exact one I have problems with:
We use the integers a, b, and n to create the following series:
(a + 2^0 * b), (a + 2^0 * b + 2^1 * b), ... ,(a + 2^0 * b + 2^1 * b + ... + 2^n-1 * b)
You are given q queries in the form of a, b, and n. For each query, print the series corresponding to the given a, b, and n values as a single line of n space-separated integers.
Input Format
The first line contains an integer, q, denoting the number of queries. Each line i of the q subsequent lines contains three space-separated integers describing the respective ai, bi, and ni values for that query.
Output Format
For each query, print the corresponding series on a new line. Each series must be printed in order as a single line of n space-separated integers.
I tried this code:
import java.util.*;
import java.lang.Math.*;
class Playground {
public static void main(String[ ] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for(int i = 0; i < q; i++) {
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
int num = a;
for(int j = 0; j < n; j++) {
num += (((int) Math.pow(2, j)) * b);
System.out.print(num + " ");
}
System.out.println();
}
}
}
But it failed the test, even though the "Expected output" and the actual output looked the same. I tried searching for other solutions, but the ones I found were not that different from my own.
Input
2
0 2 10
5 3 5
Expected Output
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
Output
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
This is almost certainly related to the trailing space in your output:
2 6 14 30 62 126 254 510 1022 2046 | <<= Trailing space
8 14 26 50 98 | <<= Trailing space
Fix your output as follows:
for(int j = 0; j < n; j++) {
if (j != 0) {
System.out.print" ");
}
num += (((int) Math.pow(2, j)) * b);
System.out.print(num);
}
Note that you can avoid calling Math.pow at all, because powers of 2 can be computed using bit shift expression 1 << j; multiplication of b by 1 << j is equivalent to shifting b left by j:
for(int j = 0; j < n; j++) {
if (j != 0) {
System.out.print" ");
}
num += (b << j);
System.out.print(num);
}
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int TestCase = sc.nextInt();
while (TestCase-- > 0) {
int a = 0, b = 0, n = 0;
a = sc.nextInt();
b = sc.nextInt();
n = sc.nextInt();
int sum = a + b;
for (int i = 1; i < n;) {
System.out.print(sum + " ");
sum += ((int) Math.pow(2, i) * b);
i++;
if (i == n) {
System.out.print(sum);
}
}
System.out.println();
}
sc.close();
}
}
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
int power = 1,sum=0;
for(int j=0;j < n;j++)
{
sum=a+(power*b);
System.out.print(sum+" ");
power = power * 2;
power++;
}
System.out.println("");
}
in.close();
}
//where 2^0*b, 2^0*b + 2^1*b, 2^0*b + 2^1*b + 2^2*b .....,2^(k+1) - 1
import java.util.*;
import java.io.*;
import java.lang.Math;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
int count = 0;
for(int j=0;j<n;j++) {
System.out.print((int)(a+b*(Math.pow(2,j + 1)-1))+" ");
}
System.out.println();
}
in.close();
}
}
for (int j = 0; j < n; j++ ){
if(j==0){
output = output + (a + (int)Math.pow(2,j) * b);
}
else{
output = output + ((int)Math.pow(2,j) * b);
}
System.out.print(output+" ");
}
System.out.println();
Please check the following simple method to solve this problem but mind it, you can decrease time complexity using recursion, here I am going without recursion:
import java.util.*;
import java.io.*;
import java.lang.Math;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
double sum=0;
for(int j=1;j<=n;j++)
{
sum=a;
for(int k=0;k<j;k++)
{
sum=sum+(b*Math.pow(2, k));
}
System.out.print((int)sum+" ");
}
System.out.println();
}
in.close();
}
}
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
int a =0,b=0,n=0;
for(int i=0;i<t;i++){
a = in.nextInt();
b = in.nextInt();
n = in.nextInt();
arrange(a,b,n);
}
in.close();
}
public static void arrange(int a,int b,int n){
int sum = a+b;
for(int j=1; j<=n; j++){
System.out.print(sum+" ");
sum+=((Math.pow(2,j))*b);
}
System.out.println();
}
}
Here's the full code with more optimized and cleaned - It will run all your test cases
import java.util.*;
import java.io.*;
class Solution
{
public static void main(String []argh)
{
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++)
{
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
int s=0;
s=s+a;
for(int j=0;j<n;j++)
{
s+=(Math.pow(2,j))*b;
System.out.print(s+" ");
}
System.out.println();
}
in.close();
}
}
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int result = 0;
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for (int j = 0; j < n; j++ ){
if(j==0){
result = result + (a + (int)Math.pow(2,j) * b);// for (a + 2^0 * b)
}
else{
result = result + ((int)Math.pow(2,j) * b); // for (a + 2^0 * b + 2^1 * b)
}
System.out.print(result+" ");
}
System.out.println();
}
in.close();
}
}
Try this completed code...
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for(int x=0; x<n; x++){
a+=(Math.pow(2,x)*b);
System.out.print(a+" ");
}
System.out.println();
}
in.close();
}
}
Related
what's wrong with this?
if condition is not executing.
import java.util.Scanner;
public class ArmstrongNum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
int n = sc.nextInt();
int temp = n;
int rem = 0;
int sum = 0;
while (n != 0) {
rem = n % 10;
sum = sum + (rem * rem * rem);
n = n / 10;
}
if (temp == n) {
System.out.println("number is a AMSTRONG");
} else {
System.out.println("NUMBER IS NOT AMSTRONG");
}
}
}
Your logic is wrong. if(temp==n) { is after while(n!=0) { so the only way it could be true is if temp == 0.
Your Calcualtion is fine but your compare is wrong. For example the number 153, which is a armstrong number, your variables will have the following values at the end of the loop:
temp = 153
rem = 1
sum = 153
n = 0
So you should not compare temp to n temp == n , how you currently do but temp to sum temp == sum
Also take in mind that your check will only work for three digits numbers, because your power is allways three. So for other armstrong numbers it simply won't work, for example:
54748 = 55 + 45 + 75 + 45 + 85 = 3125 + 1024 + 16807 + 1024 + 32768
In this solution i used the Math libary and the power but there are more ways if you don't like this to calc the length of an number
public class ArmstrongNum {
private static final Scanner SC = new Scanner(System.in);
private static boolean isArmstrongNumber(int n){
int temp = n;
int rem;
int length = (int) (Math.log10(n) + 1);
int sum = 0;
while (n != 0) {
rem = n % 10;
sum = sum + (int) Math.pow(rem, length);
n = n / 10;
}
return temp == sum;
}
public static void main(String[] args) {
System.out.print("ENTER THE NUMBER: ");
int n = SC.nextInt();
if (isArmstrongNumber(n)) {
System.out.printf("%d is a Armstrong number", n);
} else {
System.out.printf("%d is no Armstrong number", n);
}
}
}
Try another way
public static void main(String[] args) {
try {
Scanner sc= new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
int n = sc.nextInt();
if (n == 0) {
throw new Exception("Number is 0");
}
int sum = 0;
String number = String.valueOf(n);
char[] chars = number.toCharArray();
double length = chars.length;
for (char item : chars) {
double result = Math.pow(Double.parseDouble(String.valueOf(item)), length);
sum = sum + (int) result;
}
if (sum == n ) {
System.out.println("number is a AMSTRONG");}
else {
System.out.println("NUMBER IS NOT AMSTRONG");
}
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
}
I am a beginner trying to program a java code to produce output numbers that is a prime factor of two and five.
For example, if input is 8, then output should be 2 4 5 8.
However, whenever I print my output, the result will be 2 5 4 5 8 5.
Please advice on where I have gone wrong.
Thank you
import java.util.Scanner;
class twofive {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n:");
int n = sc.nextInt();
double num = 0;
double num2 = 0;
for (int i = 1; (((Math.pow(2,i))<= n) || ((Math.pow(5,i)) <=n) || (((Math.pow(2,i))<= n) && ((Math.pow(5,i)) <=n))) ; i++) {
if (( Math.pow(2,i)) <= n)
num = (Math.pow(2,i));
int convert = (int) num;{
System.out.print(convert + " ");
}
if ((Math.pow(5,i)) <= n)
num2 = (Math.pow(5,i));
int convert2 = (int) num2;
{System.out.print(convert2 + " ");
}
}
}
}
After you review #AmedeeVanGasse 's comment, you need to fix your braces.
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n:");
int n = sc.nextInt();
double num = 0;
double num2 = 0;
for (int i = 1; Math.pow(2,i))<= n) || ((Math.pow(5,i)) <=n) || (((Math.pow(2,i))<= n) && ((Math.pow(5,i)) <=n))) ; i++) {
if (( Math.pow(2,i)) <= n) {
num = (Math.pow(2,i));
int convert = (int) num;
System.out.print(convert + " ");
}
if ((Math.pow(5,i)) <= n) {
num2 = (Math.pow(5,i));
int convert2 = (int) num2;
System.out.print(convert2 + " ");
}
}
}
You should also review the logic in your for loop and if statements. They are full of unneeded redundancies.
I am new to Java and stackoverflow. I am writing a program that can add power in Java, for example: 2^1, 2^1+2^2, 2^1+2^2+2^3,.. so on.
I have written below program and I don't know what I am doing wrong when I am trying to add the powers. I just get 2^1 2^2 2^3,... as output. I hope you get the idea from my code and it will be a great help if you guys can help me learn.
Thank you in advance!
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt(); //a = first number
System.out.print("Enter b: ");
int b = sc.nextInt(); //b = second number
System.out.print("Enter t: ");
int t = sc.nextInt(); //t = no. of iterations
int x=0, sum = 0;
for (int i = 0; i < t;) {
for (int j = 0; j < t; j++) {
int pow = (int) Math.pow(2, i);
x = a + (pow * b);
i++;
System.out.printf("%d ", x);
sum = x;
}
sum = x + sum;
System.out.println(sum);
}
}
According to Mathematics rules, if it is addition among the numbers, for example 2^1 + 2^2 + 2^3 + 2^4... Then it is simple you don't need two loops and the t variable. You just need the base and the last exponent limit.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the base: ");
int a = sc.nextInt(); //a = first number
System.out.print("Enter iterations: ");
int b = sc.nextInt(); //b = No of iterations
int sum = 0;
for (int i = 1; i <= b; i++) {
sum += Math.pow(a, i);
}
System.out.println("The sum is " + sum);
}
But if there is multiplication among the numbers, then you will add the exponents if the base is same. Fox example 2^1 * 2^2 * 2^3 * 2^4.... Then you may do it as below.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the base: ");
int a = sc.nextInt(); //a = first number
System.out.print("Enter iterations: ");
int b = sc.nextInt(); //b = No of iterations
Double res;
int powerSum = 0;
for (int i = 1; i <= b; i++) {
powerSum += i;
}
System.out.println("Power sum is " + powerSum);
res = Math.pow(a, powerSum);
System.out.println("The result is " + res);
}
In your inner loop
int pow = (int) Math.pow(2, i);
shouldn't you be using j instead of i?
Very simply you can do it as below:
public static int getPow(int num, int pow) {
if (pow < 2) {
return num;
}
return (int) Math.pow(num, pow) + getPow(num, --pow);
}
Usage:
int pow = getPow(2, 4);// 2*1 + 2*2 + 2*2*2 + 2*2*2*2 = 2+4+8+16 = 30
System.out.println("pow = " + pow);
And Result:
pow = 30
I've got a problem with some simple code. I haven't seen where is the problem in my code. It returns false when it should return true, since 153 is an Armstrong Number.
Following is my code:
public class Armstrong {
static double nArms, unidad, decena, centena, aux;
Armstrong(){
}
Armstrong(double nArms){
this.nArms = nArms;
}
public boolean esArmstrong(double nArms){
aux = nArms % 100;
centena = nArms / 100;
decena = aux / 10;
unidad = aux % 10;
this.nArms = Math.pow(unidad, 3) + Math.pow(decena, 3) +Math.pow(centena, 3);
if(this.nArms == nArms){
return true;
}else{
return false;
}
}
public static void main(String[] args) {
Armstrong arms = new Armstrong();
System.out.println(arms.esArmstrong(153));
}
}
You are using double when you intend to do integer arithmetic. For example, when you write
centena = nArms / 100;
you are doing floating point division, (and centena is assigned the value 1.53) but you want to perform integer division. Use int, long (or BigInteger) instead.
As others already mentioned never use Double for Integer calculation
Now If I were you I would have optimized my code to this
int check=0;
int n=num; // num is user-input number
while(n>0)//suppose n =153
{
int rem=n%10;
check=check+(int)Math.pow(rem,3);
n=n/10;
}
if(check==num)
System.out.println(num+" is Armstrong");
/*First time while loop runs rem = 3 and n= 15
So check = 0+3*3*3=27
Second time while loop runs rem = 5 and n= 1
So check = 27+5*5*5 = 152
Again n =1 so rem = 1 and check = 152+1*1*1 = 153
This time the thw while fails the exits
ultimately check == num so it is armstrong */
import java.util.Scanner;
public class Armstrong {
public static void main(String args[]) {
System.out.println("Input number of digit to find out Armstrong");
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
String s[] = new String[r];
System.out.println("Please enter digits");
StringBuilder sb = new StringBuilder(r);
for (int i = 0; i < r; i++) {
String userInput = sc.next();
s[i] = userInput;
sb.append(s[i]);
}
int e = Integer.parseInt(sb.toString()); //this is the Integer value entered to check Armstrong number
int d;
int k[] = new int[r];
for (int j = 0; j < r; j++) {
d = Integer.parseInt(s[j]);
k[j] = d * d * d * d* d* d;
}
int m[] = new int[r + 1];
int n[] = new int[r];
for (int l = 1; l <= r; l++) {
n[l - 1] = m[l - 1] + k[l - 1];
m[l] = n[l - 1];
}
if (e == m[r]) {
System.out.println("Entered number is Armstrong number");
} else {
System.out.println("Entered number is not an Armstrong number");
}
}
public void isArmstrong(String n)
{
char[] s=n.toCharArray();
int sum=0;
for(char num:s)
{
int i=Integer.parseInt(Character.toString(num));
int cube=i*i*i;
sum +=cube;
}
if(sum==Integer.parseInt(n))
{
System.out.println("Its an Armstrong Number");
}
else
{
System.out.println("Its not an Armstrong Number");
}
}
import java.util.*;
public class Main
{
public static void check_armstrong(int n)
{
/*Function to check whether a number is an armstrong number or not
Print true if yes else false */
int sum=0;
int temp=n;
while(n>0){
int remainder=n%10;
sum+=remainder*remainder*remainder;
n=n/10;
}
if(temp==sum){
System.out.println(true);
}else
System.out.println(false);
/* Do not change the code beyond this point*/
}
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int n =sc.nextInt();
check_armstrong(n);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int a, c, d, e = 0, temp = 0;
a = sc.nextInt();
c = a;
int z = a;
while (c > 0) {
c = c / 10;
temp++;
}
System.out.println("//");
int temp2 = temp;
while (temp2 > 0) {
int temp1 = 1;
d = a % 10;
for (int i = 0; i < temp; i++) {
temp1 = temp1 * d;
}
e = e + temp1;
a = a / 10;
temp2--;
}
if (z == e) {
System.out.println("number is armstrong");
} else {
System.out.println("number is not armstrong");
}
}
import java.io.*;
public class TestCaseAbcd {
public static void main(String args[]) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(
float x0;
float a, c, mod;
int num, ch = 0;
double[] rNumbers;
double[] rTemp;
System.out.println("Enter the SEED value: ");
System.out.println("x0 ");
x0 = Float.parseFloat(stdin.readLine());
System.out.println("Enter the multiplier's value:");
System.out.println("a: ");
a = Float.parseFloat(stdin.readLine());
System.out.println("Enter the value of increment c and modulus m: ");
System.out.println("c: ");
c = Float.parseFloat(stdin.readLine());
System.out.println("m: ");
mod = Integer.parseInt(stdin.readLine());
System.out.println("How many random nunbers u need? ");
num = Integer.parseInt(stdin.readLine());
rNUmbers = new double[num];
rTemp = new double[rNumbers.length];
rTemp[0] = x0;
for (int i = 0; i < num; i++)
{
if(i + 1 != num)
{
rTemp[i + 1] = (((rTemp[i] * a) + c) % mod);
}
else
{
break;
}
}
for (int i = 0; i < rNumbers.length; i++)
{
if (i + 1 != num)
{
rNumbers[i] = rTemp[i] / mod;
}
else
{
break;
}
}
System.out.println("The PSEUDO random numbers are: ");
for (int i = 0; i < rNumbers.length; i++)
{
System.out.println(rNumbers[i]);
}
double firstNum = rNumbers[0];
System.out.println("1. Select Mutant 1 ");
System.out.println("2. Select Mutant 2 ");
System.out.println("3. Exit ");
}
}
In the above code, the expected output should start from: 0.68.
But, instead it started from:
0.37.
In fact, even after I changed the following code:
rTemp[i+1] = ( ( (rTemp[i]*a) + c ) % mod);
to:
rTemp[i+1] = ( ( (rTemp[i]/a) + c ) % mod);
The output still started from 0.37.
The input values are:
x0 = 37
a = 7
c = 9
m = 100
Please help me in analyzing the code so as that the output shouldn't start with 0.37.
Summary of the problem: the code is producing the same number i.e. 0.37 no matter what the equation stated above in the code is modified to.
You're setting the first value in rTemp to 37, then you start printing from the first value. It only makes sense that the first value printed would be 0.37.
x0 = 37
mod = 100
...
rTemp[0] = x0;
...
rNumbers[i] = rTemp[i] / mod;
...
System.out.println(rNumbers[i]);
To achieve the output you're looking for, change this:
rTemp[0] = x0;
for (int i = 0; i < num; i++)
{
if(i + 1 != num)
{
rTemp[i + 1] = (((rTemp[i] * a) + c) % mod);
}
else
{
break;
}
}
to this:
rTemp[0] = (((x0 * a) + c) % mod);
for (int i = 0; i < num-1; i++)
{
rTemp[i + 1] = (((rTemp[i] * a) + c) % mod);
}
On a side note, if all you really want is an array of psudo-random doubles, it's easier to do something like this:
Random rnd = new Random(seed);
double[] rNumbers = new double[num];
for(int i = 0; i < num; i++)
rNumbers[i] = (double)rnd.nextInt(100) / 100;
For any given seed, you'll get the same array of doubles every time.