Can someone please help me solve this? The number should be variable and not constant.
The output should be:
Timestamping In 6 Digit
8
5
6
3
0
1
Average In 6 Digit
9
8
7
6
5
2
class Timestamp1 extends Average1
{
public static void main (String args[]) {
int i = 103658;
int j = 10;
int k = i % j;
System.out.println(" Timestamping In 6 Digit " );
System.out.println(" " + k);
int o = 10365;
int p = 10;
int q = o % p;
System.out.println(" " + q);
int l = 1036;
int m = 10;
int n = l % m;
System.out.println(" " + n);
int r = 103;
int s = 10;
int t = r % s;
System.out.println(" " + t);
int u = 10;
int v = 10;
int w = u % v;
System.out.println(" " + w);
int x = 1;
int y = 10;
int z = x % y;
System.out.println(" " + z);
class Average1 extends Timestamp1 {
public void main() {
int i = 256789;
int j = 10;
int k = i % j;
System.out.println(" Average In 6 Digit ");
System.out.println(" " + k);
int o = 25678;
int p = 10;
int q = o % p;
System.out.println(" " + q);
int l = 2567;
int m = 10;
int n = l % m;
System.out.println(" " + n);
int r = 256;
int s = 10;
int t = r % s;
System.out.println(" " + t);
int u = 25;
int v = 10;
int w = u % v;
System.out.println(" " + w);
int x = 2;
int y = 10;
int z = x % y;
System.out.println(" " + z);
}
}
}
}
If you just want to reverse a number then here you go
public String reverseNum(Integer i)
{
return new StringBuilder(i.toString()).reverse();
}
What do you really want to do? Print two integer numbers reversed? Then you should say so.
public static String reverseDigits(int i) {
StringBuilder sb = new StringBuilder();
sb.append(i);
sb.reverse();
return sb.toString();
}
...
System.out.println(reverseDigits(1234567));
...
Related
How I can write the program with bitwise operators instead of if statements, which will interpret as coordinates on the number axis of end points of two intervals: A = [a1, a2] and B = [b1, b2]. The program reads from the user one number (say, x) of type int and prints whether it is true that
• x ∈ A
• x ∈ B
• x ∈ A \ B
• x ∈ B \ A
• x ∈ A ∩ B
• x ∈ A ∪ B
For example, if the defined intervals are A = [2, 4] and B = [1, 6] and the number read is x = 5, the program should print something like:
Interval A = [2, 4]
Interval B = [1, 6]
Enter x 5
x in A: false
x in B: true
x in A\B: false
x in B\A: true
x in intersection of A and B: false
x in union of A and B: true
So now this is what I have
import org.w3c.dom.ranges.Range;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("input a1");
int a1 = sc.nextInt();
System.out.println("input a2");
int a2 = sc.nextInt();
System.out.println("input b1");
int b1 = sc.nextInt();
System.out.println("input b2");
int b2 = sc.nextInt();
int a =a1&a2;
System.out.println("Interval A = " +a);
int b = b1 & b2;
System.out.println("Interval B = " + b);
System.out.println("input x");
int x = sc.nextInt();
System.out.println("Interval A = [" + a1 + "," + a2 + "]");
System.out.println("Interval B = [" + b1 + "," + b2 + "]");
System.out.println("Enter x " + x);
System.out.println("x in A: ");
System.out.println("x in B: ");
System.out.println("x in A/B: ");
System.out.println("x in B/A: ");
System.out.println("x in intersection of A and B: ");
System.out.println("x is union of A and B: ");
System.out.println("x in symm. diff. of A and B: ");
}
}
You can write this (just use Array in Java):
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("input a1: ");
int a1 = sc.nextInt();
System.out.print("input a2: ");
int a2 = sc.nextInt();
System.out.print("input b1: ");
int b1 = sc.nextInt();
System.out.print("input b2: ");
int b2 = sc.nextInt();
System.out.print("input x: ");
int x = sc.nextInt();
int arr1[] = new int [a2-a1+1];
int arr2[] = new int [b2-b1+1];
for (int i = 0, j = a1; j <= a2; i++, j++) {
arr1[i] = j;
}
for (int i = 0, j = b1; j <= b2; i++, j++) {
arr2[i] = j;
}
System.out.println("Interval A: [" + a1 + ", " + a2 + "]");
System.out.println("Interval B: [" + b1 + ", " + b2 + "]");
System.out.println("Enter x: " + x);
System.out.println("x in A: " + Exist(arr1, x));
System.out.println("x in B: " + Exist(arr2, x));
System.out.println("x in A/B: " + Relative_Complement(arr1, arr2, x));
System.out.println("x in B/A: " + Relative_Complement(arr2, arr1, x));
System.out.println("x in intersection of A and B: " + Intersection(arr1,arr2, x));
System.out.println("x is union of A and B: " + Union(arr1, arr2, x));
}
static boolean Exist(int arr[], int x){
for (int i = 0; i < arr.length; i++) {
if(x == arr[i])
return true;
}
return false;
}
static boolean Relative_Complement(int arr1[], int arr2[], int x){
int arr3[] = new int [arr1.length + arr2.length + 1];
int arr4[] = new int [arr1.length + arr2.length + 1];
for (int k = 0, i = 0; i < arr2.length; i++){
for (int j = 0; j < arr1.length; j++) {
if(arr2[i] == arr1[j]){
arr3[k++] = arr2[i];
}
}
}
for (int p = 0, j = 0; j < arr1.length; j++) {
if(!Exist(arr3, arr1[j])){
arr4[p++] = arr1[j];
}
}
return Exist(arr4, x);
}
static boolean Union(int arr1[], int arr2[], int x){
int arr3[] = new int [arr1.length + arr2.length];
int k = 0;
for (int i = 0; i < arr1.length; i++, k++)
arr3[k] = arr1[i];
for (int i = 0; i < arr2.length; i++, k++)
arr3[k] = arr2[i];
return Exist(arr3, x);
}
static boolean Intersection(int arr1[], int arr2[], int x){
int arr3[] = new int [arr1.length + arr2.length];
for (int i = 0; i < arr2.length; i++){
for (int j = 0; j < arr1.length; j++) {
if(arr2[i] == arr1[j]){
arr3[i] = arr2[i];
}
}
}
return Exist(arr3, x);
}
}
Notice: According to your question, a1 < a2 and b1 < b2.
Also, if you read HashSet you can solve this problem easier.
I have implemented a solutions for multiplying any two large numbers, the solution I am getting seems to be working fine for many inputs however when I pass
"3141592653589793238462643383279502884197169399375105820974944592"
"2718281828459045235360287471352662497757247093699959574966967627"
It's not giving correct answer
I am using Basic Java Syntax
import java.util.*;
public class GenericOperations {
static String addTwoLargeNumber(String X, String Y)
{
String Z = "";
int i = X.length() - 1;
int j = Y.length() - 1;
int carry = 0;
while(i>-1 && j>-1)
{
int digitSum = Character.getNumericValue(X.charAt(i)) +
Character.getNumericValue(Y.charAt(j)) + carry;
if(digitSum > 9)
{
Z = digitSum%10 + Z;
carry=1;
}
else
{
Z = digitSum + Z;
carry = 0;
}
i--;j--;
}
while(i>-1)
{
int digitSum = Character.getNumericValue(X.charAt(i)) + carry;
if(digitSum > 9)
{
Z = digitSum%10 + Z;
carry=1;
}
else
{
Z = digitSum + Z;
carry = 0;
}
i--;
}
while(j>-1)
{
int digitSum = Character.getNumericValue(Y.charAt(j)) + carry;
if(digitSum > 9)
{
Z = digitSum%10 + Z;
carry=1;
}
else
{
Z = digitSum + Z ;
carry = 0;
}
j--;
}
return Z;
}
static String multiplyTwoLargeNumbers(String X, String Y)
{
ArrayList<String> additionList = new ArrayList<String>();
ArrayList<String> additionListWithZeros = new ArrayList<String>();
int n = X.length() - 1;
int m = Y.length() - 1 ;
int carry = 0;
int i=0,j=0;
for(i=n ; i>-1; i--)
{
carry = 0;
String Z = "";
for(j = m ; j>-1 ; j--)
{
int digitSum = ( Character.getNumericValue(X.charAt(i)) *
Character.getNumericValue(Y.charAt(j)) )
+ carry;
if(digitSum > 9)
{
Z = digitSum%10 + Z;
carry= digitSum/10;
}
else
{
Z = digitSum + Z;
carry = 0;
}
}
if(carry!=0)
{
Z = carry + Z;
}
additionList.add(Z);
}
int k = 0;
String sum = "";
for (Iterator<String> iterator = additionList.iterator(); iterator.hasNext();)
{
String val1 = iterator.next();
for(int x = 0;x<k;x++)
{
val1 = val1 + 0;
}
k++;
//System.out.println(val1);
sum = addTwoLargeNumber(sum,val1);
}
return sum;
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String args[])
{
String val = multiplyTwoLargeNumbers(
"3141592653589793238462643383279502884197169399375105820974944592",
"2718281828459045235360287471352662497757247093699959574966967627");
System.out.println(val);
}
}
Expected Result:
8539734222673567065463550869546574495034888535765114961879601127067743044893204848617875072216249073013374895871952806582723184
Actual Results:
8539734222673566965463549869546574495034887534765114961879601127067743044893204848617875072216249073013374895871952806582723184
You just missed a carry when adding.
Add following and you should be done:
if (carry != 0) {
Z = carry + Z;
}
BTW: simplified the addTwoLargeNumber-method
static String addTwoLargeNumber(String X, String Y) {
String Z = "";
int maxPos = Math.max(X.length(), Y.length());
int carry = 0;
for (int pos = 0; pos < maxPos; pos++) {
final int currValX
= pos < X.length()
? Character.getNumericValue(X.charAt(X.length() - 1 - pos)) : 0;
final int currValY
= pos < Y.length()
? Character.getNumericValue(Y.charAt(Y.length() - 1 - pos)) : 0;
int digitSum = currValX + currValY + carry;
Z = digitSum % 10 + Z;
carry = digitSum / 10;
}
if (carry != 0) {
Z = carry + Z;
}
return Z;
}
My problem is:
My math formula is:
In this case X = N; Y = L;U = K;
public class Play {
public static void main(String args[]) {
//n!(n−k−1)!
int n = 10;
int k =2;
int l = 12;
long result;
result = (calculaFator(n) / calculaFator(n-k-1));
result= (long) (result * Math.pow((n-k),(l-k)-1));
System.out.println(result);
}
public static long calculaFator(long x) {
long f = x;
while (x > 1) {
f = f * (x - 1);
x--;
}
return f;
}
}
It should be 721599986, but it is giving me 96636764160
I have some samples:
With n=10, k=2, l=12 it should be 721599986
With n=10, k=2, l=16 it should be 626284798
With n=10, k=1, l=20 it should be 674941304
With n=5, k=2, l=8 it should be 10800
The java codes is working according to your stated formula.
It seems like the formula is wrong rather than the codes. (or expected results or your x,u,y mapping to n,l,k is incorrect?)
int x = 10;
int u = 2;
int y = 12;
long numerator = calculaFator(x);
long denominator = calculaFator(x - u - 1);
int xu1 = x - u - 1;
long result = numerator / denominator;
System.out.println();
System.out.println(x + "!= numerator: " + numerator); //10!= numerator: 3_628_800
System.out.println(xu1 + "!= denominator: " + denominator); //7!= denominator: 5_040
System.out.println("result1: " + result); //result1: 720 (correct)
int xu = x - u;
int yu1 = y - u - 1;
double remainderPlaylist = Math.pow(xu, yu1);
System.out.println(xu + "^" + yu1 + " = " + remainderPlaylist);//8^9 = 1.34217728E8
System.out.println(xu + "^" + yu1 + " = " + (long) remainderPlaylist);//8^9 = 134_217_728 (correct)
long mul = (long) (result * remainderPlaylist);
System.out.println(result + "x" + (long)remainderPlaylist + " = " + mul); //720x134_217_728 = 96_636_764_160 (mathematically correct)
public class BinaryToDecimal{
public static void main(String[]args){
int binary = 101011101;
int d = 10;
int l = 1;
for (int j=1;j<=8;j++){
int r = (binary/d%10)*l;
int k = r;
int z = r + k;
d*=10;
l*=2;
}
System.out.println("The binary number " + binary + " is equivalent to "+ z +" in decimal."); // z here is out of scope of for
}
}
I need to finish this assignment using only the for loop, but I can't find a working code... This is the best i did.
Here is a pretty simple algorithm using the 2^x logic.
What it is basically doing is :
If we encounter 1 in the binary variable, we add 2^(Position of the 1) to the decimal variable
class MyClass{
public static void main(String[] args) {
int bin = 101;
String binary = String.valueOf(bin);
int decimal = 0;
for (int i = 0 ; i < binary.length() ; i++){
if (binary.charAt(i) == '1'){
decimal += Math.pow(2, binary.length() - i - 1);
}
}
System.out.println(decimal);
}
}
public class BinaryToDecimal{
public static void main(String[]args){
int binary = 101011101;
int powerOfTen = 10;
int powerOfTwo = 1;
int Decimal = 0;
for (int j=1;j<=8;j++){
int splitAnswer = (binary/powerOfTen%10)*powerOfTwo;
int old = splitAnswer;
Decimal = splitAnswer + old;
powerOfTen*=10;
powerOfTwo*=2;
}
System.out.println("The binary number " + binary + " is equivalent to "+ Decimal +" in decimal.");
}
}
I have a very challenging problem here today. I cannot think of a way to solve it.
Given 6 numbers as input: a1, a2, a3, b1, b2, b3, find 2 numbers X and Y such that a1 * x^2 + a2 ^ x + a3 = b1 * y^2 + b2 * y + b3. X and Y must be between 10 and 15000 inclusive.
What I have tried:
I have tried all X values from 10-15000 and all Y values from 10-15000, and checked if they satisfied the equation. However, this method is extremely slow. Does anyone have a faster solution? Thanks.
My Bad Code:
for (int i = 0; i < k; i++) {
int a, b;
cin >> a >> b;
for (int i = 10; i <= 15000; i++) {
for (int j = 10; j <= 15000; j++) {
if (conv(a, i) == conv(b, j)) {
cout << i << " " << j << endl;
j = 20000;
i = 20000;
}
}
}
}
long long conv(int x, int b) {
long long ans = 0;
int count = 0;
while (x) {
int y = x % 10;
ans += y * poww(b, count);
count++;
x /= 10;
}
return ans;
}
long long poww(int x, int y) {
long long ans = 1;
while (y != 0) {
ans *= x;
y--;
}
return ans;
}
I thought this might be a good occassion to exercise writing some Java code and came up with the following solution. On my system it gives the solution to the two numbers 419 and 792 (as you wrote in an earlier edit of your question the result should be Base X: 47 Base Y: 35) in 1 ms.
The code just uses some smart brute force :).
See it running online.
public class TwoBases {
public static void main(String[] args) {
long beg = System.nanoTime();
solve(419, 792);
System.out.println("Time needed to calculate: "+(System.nanoTime()-beg)/1000000.0 + "ms");
}
public static void solve(int a, int b) {
int[] aDigits = new int[3];
int[] bDigits = new int[3];
for (int i = 0; i < 3; i++) {
aDigits[2 - i] = (a / (int) Math.pow(10, i)) % 10;
bDigits[2 - i] = (b / (int) Math.pow(10, i)) % 10;
}
for (int x = 10; x <= 15000; x++) {
int numBaseX = digitsToBase10(aDigits, x);
int y = 10;
while (y <= 15000) {
int numBaseY = digitsToBase10(bDigits, y);
if (numBaseX == numBaseY) {
System.out.println("Base X: " + x + " Base Y: " + y);
return;
} else if (numBaseY > numBaseX) {
break;
} else {
y++;
}
}
}
System.out.println("Nothing found");
}
public static int digitsToBase10(int[] digits, int b) {
int res = 0;
for (int i = 0; i < digits.length; i++) {
res += digits[i] * (int) Math.pow(b, digits.length - 1 - i);
}
return res;
}
}