I need help with creating a VeryLargeInteger class similar to the BigInteger however, as part of my assignment I am not allowed to use BigInteger. I have started off by storing large numbers as strings and then converting them to int[] to perform mathematical functions with them. The problem I am running into is working with two different sized arrays such as:
int[] a = {1, 2, 3, 4, 5} // represents 12,345
int[] b = {1, 2, 4} //represents 124
When I add them I get:
int[] c = {2, 4, 7, 4, 5}
instead of
int[] c = {1, 2, 4, 6, 9}
This is a little messy.
import java.util.Arrays;
public class VeryLargeInteger
{
int[] test, test2;
String temp, temp2;
int size;
VeryLargeInteger(int[] input)
{
int[] test = input;
System.out.println(Arrays.toString(test));
}
VeryLargeInteger(long input1)
{
long input = input1;
temp = Long.toString(input1);
test = convert(temp);
System.out.println(Arrays.toString(test));
}
VeryLargeInteger(String input1)
{
temp = input1;
test = convert(input1);
System.out.println(Arrays.toString(test));
}
public static int[] convert(String input)
{
int [] array = new int[input.length()];
for (int i = 0; i < input.length(); i++)
{
int value = input.charAt(i) - '0';
array[i] = value;
}
return array;
}
VeryLargeInteger add(VeryLargeInteger other)
{
int max = Math.max(this.temp.length(), other.temp.length());
int[] result = new int[max];
int carry = 0;
for (int i = 0; i < max; ++i)
{
int a = i < this.test[i] ? this.test[this.test[i] - i -1] : 0;
int b = i < other.test[i] ? other.test[other.test[i] - i -1] : 0;
int sum = a + b + carry;
carry = sum / 10;
sum -= carry;
result[result.length - i - 1] = sum;
}
VeryLargeInteger added = new VeryLargeInteger(result);
return added;
}
/*
VeryLargeInteger sub(VeryLargeInteger other)
{
}
VeryLargeInteger mul(VeryLargeInteger other)
{
}
VeryLargeInteger div(VeryLargeInteger other)
{
}
VeryLargeInteger mod(VeryLargeInteger other)
{
}
static String toString(VeryLargeInteger other)
{
}*/
public static void main(String[] args)
{
VeryLargeInteger a = new VeryLargeInteger(1050L);
VeryLargeInteger b = new VeryLargeInteger("121123");
VeryLargeInteger c = a.add(b);
}
}
You could pad the second array to get them to add properly:
int[] a = {1, 2, 3, 4, 5};
int[] b = {0, 0, 1, 2, 4};
Then, if your add method compares a[0] to b[0] and so forth, you will get {1, 2, 4, 6, 9}.
An important note I would like to make is this: You are not using the benefit of integers. Integers are 32 bits long, which are 32 digits in base 2. Now, you are using an integers as 1 digit in base 10. By doing this, you are only using 0.0000002% of the memory in a useful way. Use the full range that integers support. The point would be to make an array of integers, but where each integer effectively represents 32 bits of the actual number you want to hold. Adding them together then just goes component-wise, and take care of overflow by using a carry.
To fix the problem as you are facing it now: Align your arrays right. Do not add leading zeroes as Mike Koch suggests, but align them properly, by basically accessing the array elements from back to front, instead of from front to back, as you are doing now. By looking at your code, you have attempted that, but you are having trouble with ArrayIndexOutOfBoundsExceptions I guess. Access both components of the array like this:
int[] number0 = ...;
int[] number1 = ...;
int max = Math.max(number0.length + number1.length) + 1;
int[] result = new int[max];
int carry = 0;
for (int i = 0; i < max; ++i)
{
int c0 = i < number0.length ? number0[number0.length - i - 1] : 0;
int c1 = i < number1.length ? number1[number1.length - i - 1] : 0;
int sum = c0 + c1 + carry;
carry = sum / 10;
sum -= carry;
result[result.length - i - 1] = sum;
}
Related
The title may be a bit confusing so here's an instance. I have two arrays:
int [] scores;
scores = new int[5]; //(5,7,10,3,6)
int [] places;
places = new int[5]; //(1,2,3,4,5)
I need to somehow sort the second array (I can't change the first one), so it represents the highness of elements in the first array. 10 is the highest so its place has to be 1st, 3 is the lowest so its place has to be 5th.
After the sorting second array should look like this:
places = {4,2,1,5,3};
Here's my code, and I need some help to make it work the way it should.
do {
for (int i = 0; i < 5; i++) {
for (int j = 1; j < 5; j++) {
if (scores[i] < scores[j]) {
temp = places[i];
places[i] = places[j];
places[j] = temp;
flag = true;
} else {
flag = false;
}
}
}
} while (flag);
Thanks in advance
#Korashen adviced a pretty good solution,
Another way:
assume all the values of scores are different and positive, you can make a copy of the array,sort it, and by subtaction to know the indexes,
in your example:
before sorting :
scores = (5,7,10,3,6)
after sorting :
scores_sorted = (3,5,6,7,10)
the value of places will be by the following rule:
if(scores_sorted[i]-scores[j] == 0)
places[i] = j
full example:
int[] scores = new int[]{5, 7, 10, 3, 6};
int[] scores_sorted = scores.clone();
int[] places = new int[]{0,1,2,3,4};
sort(scores_sorted);
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
if(scores_sorted[i]-scores[j] == 0){
places[i] = j;
}
}
}
You can use any sorting algorithm over the places but, instead comparing the places, compare the scores indexed by places.
Here is the modified quickSort:
static int partition(int[] iarray, int[] varray, int begin, int end) {
int pivot = end;
int counter = begin;
for (int i = begin; i < end; i++) {
if (varray[iarray[i]] < varray[iarray[pivot]]) {
int temp = iarray[counter];
iarray[counter] = iarray[i];
iarray[i] = temp;
counter++;
}
}
int temp = iarray[pivot];
iarray[pivot] = iarray[counter];
iarray[counter] = temp;
return counter;
}
public static void quickSort(int[] iarray, int[] varray, int begin, int end) {
if (end <= begin) return;
int pivot = partition(iarray, varray, begin, end);
quickSort(iarray, varray, begin, pivot - 1);
quickSort(iarray, varray, pivot + 1, end);
}
The only change is add the varray argument and the comparison iarray[i] < iarray[pivot] to varray[iarray[i]] < varray[iarray[pivot]].
NOTE: places must be numbers from 0 to n - 1.
If places are keys instead indexes, you need an intermediate Map to convert the varray[iarray[i]] to varray[real_index_of.get(iarray[i])].
A running example could be:
int[] scores = new int[]{5, 7, 10, 3, 6};
int[] places = new int[]{0, 1, 2, 3, 4};
quickSort(places, scores, 0, places.length - 1);
System.out.println(Arrays.stream(scores).mapToObj(Integer::toString).collect(joining(", ")));
System.out.println(Arrays.stream(places).mapToObj(Integer::toString).collect(joining(", ")));
With output:
5, 7, 10, 3, 6
3, 0, 4, 1, 2
(your output is wrong since 5 is the second lowest value)
I'm really new to Java and there's something wrong with the code. No errors were detected, but the output is odd.
The goal is to move the data in an array to the left. For example:
x = {1,2,3}
the new array should be {2,3,1}.
Now the code below only gives me {0,0,0}. It'd be nice if you point out the mistake and tell me what to do. Thanks a lot beforehand!
public class Project1 {
public static int[] shiftone(int[]n,boolean left) {
n = new int[n.length];
int save,save2;
if(left = true){
save = n[0];
save2 = n[(n.length-1)];
for (int i = 1; i < n.length-1; i++) {
n[i-1]=n[i];
}
n[n.length-1] = save;
n[n.length-2] = save2;
}
else{
save = n[n.length-1];
for (int i=0;i<(n.length-1);i++)
n[(n.length)-i] = n[(n.length-1)-1];
n[0] = save;
}
return n;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[] x;
int k;
boolean left;
System.out.print("Masukkan jumlah data yang ingin diinput: ");
k = input.nextInt();
System.out.println();
x = new int[k];
for (int i = 0; i < k; i++) {
System.out.print("Input data ke-"+i+": ");
x[i] = input.nextInt();
}
System.out.print("Array: "+Arrays.toString(x));
System.out.println();
System.out.print("Move to left? (true/false): ");
left = input.nextBoolean();
System.out.println();
int[] y;
y = new int[k];
y = shiftone(x,left);
System.out.print("New array: "+Arrays.toString(y));
}
}
As a simple solution for your goal, you can use this
public static int[] shiftone(int[] n, boolean left) {
// you don't need to shift anything if length = 1
if (n.length < 2) {
return n;
}
if (left) {
// save first element
int save = n[0];
for (int i = 1; i < n.length; i++) {
// shift from 1 to n
n[i-1] = n[i];
}
// insert saved element to array
n[n.length - 1] = save;
} else {
// the same
int save = n[n.length - 1];
for (int i = 1; i < n.length; i++)
n[n.length - i] = n[(n.length - 1) - i];
n[0] = save;
}
return n;
}
There is the very fast method to copy the array elements from one place to another. I don't know if this will be helpful to you since it seems to me your question is homework assignment. Nevertheless, I'll put the code with appropriate comments...
public class Answer {
public static void main(String[] args) {
//test case
int[] input = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(input));
//save the first element in the temporary variable
int temp = input[0];
//the fastest way to copy the array elements
//1st parameter is the source array
//2nd parameter is the source position (read: from which element to copy)
//3rd parameter is the destination (in this case the same array)
//4th parameter is the destination position (read: where to store the 1st element)
//5th parameter is the length of elements to copy (read: how many)
System.arraycopy(input, 1, input, 0, input.length - 1);
//finally store the saved element to the end
input[input.length - 1] = temp;
System.out.println(Arrays.toString(input));
}
}
If we don't want to code the moving on our own, we can use the method Collections.rotate . It takes a List and rotates the elements by a given distance. To use it, we need to convert the int array to a List<Integer>. The rotated list is converted back to an int array.
protected static int[] move(int[] input, int distance) {
List<Integer> inputList = Arrays.stream(input).boxed().collect(Collectors.toCollection(ArrayList::new));
Collections.rotate(inputList, distance);
return inputList.stream().mapToInt(Integer::intValue).toArray();
}
Usage:
public static void main(String[] args) throws Exception {
int[] input = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
int moveLeftOnce = -1;
int[] moved = move(input, moveLeftOnce); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
}
Please note:
Since Collections.rotate will move the elements in the given list, the list has to be modifiable. This is the case for an ArrayList. Therefore the code uses Collectors.toCollection(ArrayList::new) since there are (JavaDoc)
no guarantees on the type, mutability ... of the List returned
by Collectors.toList.
This question already has answers here:
Make individual array values to single number in Python [closed]
(3 answers)
Closed 6 years ago.
Here is my array, it consists of array of integers. Actually, these are the key of my HashMap which consists of some sentences or say "STRING" of a complete paragraph as a key value pair. Now I wanted to join those sentences from taking the key from the integer array one after another in given order.
int[] arr = {3, 2, 0, 5, 3};
HashMap<Integer, String> h = new HashMap<Integer, String>() {{
put(0,"This is my third sentence.");
put(3,"This is my first sentence.");
put(5,"This is my forth sentence.");
put(2,"This is my second sentence.");
}};
The final output should be all the sentences combined as mentioned order and outout should be like a paragraph as :
This is my first sentence.This is my second sentence.This is my third sentence.
This is my forth sentence.This is my first sentence.
Instead of converting the value to a character type you can perform math. For each digit in the array, the corresponding power of 10 is the array length (minus one) minus the index (because Java arrays use 0 based indexing and the last digit corresponds to 100). Something like,
int[] arr = { 3, 2, 0, 5, 3 };
int result = 0;
for (int i = 0; i < arr.length; i++) {
result += arr[i] * Math.pow(10, arr.length - i - 1);
}
System.out.println(result);
Output is (as expected)
32053
Optimization
It's possible to optimize the code further by keeping the current power of ten and dividing 10 while iterating each digit. This would also allow the use of a for-each loop like
int[] arr = { 3, 2, 0, 5, 3 };
int result = 0;
int pow = (int) Math.pow(10, arr.length - 1);
for (int digit : arr) {
result += digit * pow;
pow /= 10;
}
System.out.println(result);
Alternatively, iterate the digits from right to left and multiply pow by 10 on each iteration. That might look something like,
int result = 0;
int pow = 1;
for (int i = arr.length - 1; i >= 0; i--) {
result += arr[i] * pow;
pow *= 10;
}
And the above might also be written like
int result = 0;
for (int i = arr.length - 1, pow = 1; i >= 0; i--, pow *= 10) {
result += arr[i] * pow;
}
int number = Integer.parseInt(Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining()));
Yet another way:
int[] arr = {3, 2, 0, 5, 3};
int i = Integer.parseInt(Arrays.toString(arr).replaceAll("[\\[,\\] ]", ""));
System.out.println(i); // prints 32053
Though fairly simple, you should have tried yourself.
Still providing a solution, just debug and understand it.
Working Code
public static void main(String[] args) throws Exception {
int[] arr = {3, 2, 0, 5, 3};
StringBuilder numberStr = new StringBuilder();
for (int item : arr) {
numberStr.append(item);
}
int finalInt = Integer.parseInt(numberStr.toString());
System.out.println(finalInt);
}
Output
32053
First convert the array into string by appending elements one by one and the convert string into integer. Try this code:
public class NewClass63 {
public static void main(String args[]){
int[] arr = {3, 2, 0, 5, 3};
StringBuffer s = new StringBuffer();
for(int i=0;i<arr.length;i++){
s.append(arr[i]);
}
int x = Integer.parseInt(s.toString());
System.out.println(x);
}
}
int[] array = {3,2,0,5,3};
String x = "";
for(int i = 0;i<=array.length-1;i++){
x = x + String.valueOf(array[i]);
}
System.out.println(Integer.parseInt(x));
use a loop:
int[] arr = { 3, 2, 0, 5, 3 };
String itotal = "";
for (int i = 0; i < arr.length; i++)
{
itotal=itotal + String.valueOf(arr[i]);
}
int a = Integer.parseInt(itotal);
There exist various ways.
If I am right, hidden assumption is that the higher element of integer array matches with higher digit of result integer.
int[] arr = {3, 2, 0, 5, 3};
int result = 0;
int power = (int) Math.pow(10, arr.length-1);
for(int element : arr){
result += element * power;
power /= 10;
}
Easiest solution answer is this.
Assume, each alphabet in the example is a single digit.
Empty Array {} : Expected value = 0
{a}: Expected value = a
{a,b}: Expected value = 10*a + b
{a,b,c}: Expected value = 10 * (10*a + b) + c
Code: Test this is online java compiler IDE
public class ConvertDigitArrayToNumber {
public static void main(String[] args) {
int[] arr = {3, 2, 0, 5, 3};
int value = 0;
for (int i = 0; i < arr.length; i++) {
value = (10*value) + arr[i];
}
System.out.println(value);
}
}
This is actually simpler and better solution than the other ones.
Only simple multiplication and addition (No powers).
No String conversions
I'm writing a void function fibFill which fills an array with Fibonacci numbers. It doesn't have to return anything.
Here's what I have so far:
void fibFill(int[] fibo) {
fibo[0] = 1;
fibo[1] = 1;
for (int i = 2; i < fibo.length; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
}
int pos(int position) {
return fibo[pos];
}
}
For example, if I pass an array of length 5 to the method, it will override the contents of the passed array like this: [1, 1, 2, 3, 5]
Your fibFill method shouldn't have a pos method embedded in it; and I would make it static (so it can be called without an instance), like
static void fibFill(int[] fibo) {
fibo[0] = 1;
fibo[1] = 1;
for (int i = 2; i < fibo.length; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
}
}
Then you can test it with something like
public static void main(String[] args) {
int[] fib = new int[10];
fibFill(fib);
System.out.println(Arrays.toString(fib));
}
Which outputs (as requested) the fibonacci values starting at 1
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
fixed:
static void fibFill(int [] fibo) {
if(fibo.length==0) return;
if(fibo.length==1)
{
fibo[0]=1;
return;
}
fibo[0] = 1;
fibo[1] = 1;
for (int i = 2;i < fibo.length; i++)
{
fibo[i]=fibo[i-1]+fibo[i-2];
}
}
//there is no need for pos, as you can get it by type fibo[index]
Note that this solution cost you O(n), you can also do it directly by formula and there is also more ways to calculate fibonacci. for more information:
five ways to calculate fibonacci
if (n==0||n==1) {
return n;
}
int smallassumption=fib(n-1);
int smallassumption2=fib(n-2);
int ans=smallassumption+smallassumption2;
return ans;
I'm trying to take an array of any length of ints, and concatenate it into a single number without adding it up. For instance, if I have an array that goes as follows
[ 1, 7, 12, 16, 3, 8]
I want to have a variable that will equal 17121638, not equal 47.
I'm supposed to take a input of string and change it into an int without using Interger.parseInt() on the whole input itself.
This is my current attempt:
public static String toNum(String input) {
String [] charArray = new String [input.length()];
int [] parsedInput = new int [input.length()];
for(int i; i < input.length(); i++){
charArray[i] = input.substring(i);
}
for(int c; c < charArray.length; c++){
parsedInput[c] = Integer.parseInt(charArray[c]);
}
Try this:
int[] nums = { 1, 7, 12, 16, 3, 8 };
StringBuilder strBigNum = new StringBuilder();
for (int n : nums)
strBigNum.append(n);
long bigNum = 0;
long factor = 1;
for (int i = strBigNum.length()-1; i >= 0; i--) {
bigNum += Character.digit(strBigNum.charAt(i), 10) * factor;
factor *= 10;
}
Now the bigNum variable contains the value 17121638; in this case it was easier to work with strings. Be careful, if the input array is too big (or the numbers are too big) the resulting value won't fit in a long.
Try to think about how the index of the left most number relates to it's power of 10 in the final number that you're trying to achieve. For example: having an array of [1, 2, 3, 4] should produce the integer 1234. How does the base 10 power of 1000 relate to the length of the array and the index of 1?
I phrase this as a question because I get the sense that this is a homework problem for school .
Variant of Óscar López's solution that doesn't use any parsing (as the question requested):
public class BigNum {
public static void main(String[] args) {
int[] nums = {1, 7, 12, 16, 3, 8};
System.out.println(concatNums(nums));
}
public static long concatNums(int[] nums) {
long result = 0;
long multiplier = 1;
for (int i = nums.length; i > 0; --i) {
int num = nums[i - 1];
while (num != 0) {
result += multiplier * (num % 10);
num /= 10;
multiplier *= 10;
}
}
return result;
}
}
This won't work correctly if the array contains a 0. If that's important, let me know and I'll tweak the algorithm to accommodate it.
Add the numbers together by the join method - that will give you a string and then convert it into a number by the Number function.
you can simply run:
var arr = [ 1, 7, 12, 16, 3, 8];
var num = arr.join('');
Number(num);
First, you want to return an int -- not a String!
Second -- technically -- you could use Integer.valueOf(s); which returns an Integer object with the exact value that .parseInt() would've returned... though I suspect that this would be considered "cheating."
Thirdly, I think this will help you along your way: http://nadeausoftware.com/node/97
Particularly the section titled:
Parsing an integer from a string with custom code
You can use StringBuilder to append each int num from numbers (int[]).
StringBuilder sb = new StringBuilder();
for (int num : numbers) {
sb.append(num);
}
return sb.toString();
one line solution
String str = Arrays.toString(a).replaceAll("\\D", "");
Alright, here's another approach. Although this is similar to another answer, this one can handle some zeros. Just math, no use of Integer.parseInt:
public static int intsToInt(int[] input) {
int total = 0;
int multiplier = 1;
for (int i=input.length-1; i > -1; i--) {
if (input[i] == 0) {
multiplier *= Math.pow(10, String.valueOf(input[i]).length());
}
else {
total += input[i]*multiplier;
multiplier *= Math.pow(10, String.valueOf(input[i]).length());
}
}
return total;
}
public static void main(String[] args) {
int[] ints = {1, 7, 12, 0, 16, 3, 8};
out.println(digitsToInt(ints));
out.println(intsToInt(ints));
}
Here is a simple implementation. Correct if it's wrong please (I'm a beginner).
int concatArray(int[] array){
int sum = 0;
int len = array.length-1;
for(int i=0; i<array.length;i++){
sum += (Math.pow(10, len) * array[i]);
len--;
}
return sum;
}