I have 3 integer as input: a, b and c.
I'm trying to create a function to calculate the number progress at which a match will occur.
I used the following code to output the number and then the combination of input numbers. I'm trying to create a function where I can input a, b, and, c and it will return the progress for which x==a,y==bandz==c`.
int progress = 0;
for (int x = 0; x <= 160; x++) {
for (int y = 0; y <= 160; y++) {
for (int z = 0; z <= 160; z++) {
progress++;
System.out.println(progress + " " + x + " " + y + " " + z);
}
}
}
Your formula is:
progress = 1 + z_iterations
progress = 1 + z + 161*y_iterations
progress = 1 + z + 161*( y + 161*x_iterations )
progress = 1 + z + 161*( y + 161*x )
Final form:
progress = 1 + z + 161*y + 25921*x
You method should be:
public int computeProgress (int a, int b, int c) {
return 1 + c + 161*b + 25921*a;
}
Related
I'm trying to solve for the eigenvectors of a 2x2 matrix. As of right now, I'm only considering real matrices whose eigenvectors and eigenvalues are also real. I'm currently having issues solving for the eigenvectors.
This is what I have so far:
public double[] getBasis(double[][] basis){
double a = basis[0][0];
double b = basis[0][1];
double c = basis[1][0];
double d = basis[1][1];
double eigenvalue1 = ((a+d) + Math.sqrt( Math.pow(a-d,2) + 4*b*c))/2;
double eigenvalue2 = ((a+d) - Math.sqrt( Math.pow(a-d,2) + 4*b*c))/2;
double tempx;
double tempy;
int counter = 1;
for (double y = -1000; y <= 1000; y++) {
for (double x = -1000; x <= 1000; x++) {
if (((a-eigenvalue1)*x + b*y == 0) && (c*x + (d-eigenvalue1)*y == 0)) {
tempx = x;
tempy = y;
System.out.println("Eigenvector1: (" + x + "," + y + ")");
System.out.println("Eigenvalue1: "+ eigenvalue1);
}
}
}
for (double y = -10; y <= 10; y++) {
for (double x = -10; x <= 10; x++) {
if (((a-eigenvalue2)*x + b*y == 0) && (c*x + (d-eigenvalue2)*y == 0)) {
tempx = x;
tempy = y;
System.out.println("Eigenvector2: (" + x + "," + y + ")");
System.out.println("Eigenvalue2: " + eigenvalue2);
}
}
}return eigenvector1;
}
}
This method should have an input of a 2x2 array and i want it to output the two normalized eigenvectors. How would I be able to output both the eigenvectors?Additionally, I'm not allowed to use any packages that can solve for the eigenvectors or values. Basic math and arithmetic is perfectly acceptable.
Output:
Eigenvector1: (0.0,0.0)
Eigenvector1: (1.0,1.0)
Eigenvector1: (2.0,2.0)
Eigenvector1: (3.0,3.0)
Eigenvector1: (4.0,4.0)
Eigenvector1: (5.0,5.0)
Eigenvector1: (6.0,6.0)
Eigenvector1: (7.0,7.0)
Eigenvector1: (8.0,8.0)
Eigenvector1: (9.0,9.0)
Eigenvector1: (10.0,10.0)
Eigenvector2: (0.0,0.0)
How would I be able to choose only one vector for eigenvector 1 and eigenvector 2. Additionally, If the matrix had input (1,1,1,-1) such that it was a 2x2 matrix then the eigenvalues should be the square root of positive and negative 2. I'm able to get that far. However, once I attempt to calculate the eigenvectors I don't get a value for an eigenvector.
**Edited: I added the errors and took out the infinite loop I got stuck on before
**
There is no need to solve the linear system for the eigenvectors with a trial-and-error loop.
The equation
(a-e)*x+b*y == 0
has always the solution
x = b, y = -(a-e)
which you then would have to normalize. You will need to check if the first equation has the coefficients all zero, then you have to use the second equation
c*x + (d-e)*y == 0
with solution
x = -(d-e), y = c
If also the second equation has all zero coefficients, then any vector is an eigenvector, as the matrix is the diagonal matrix diag([e, e]).
This should result in some code like
e = eigenvalue1;
x = b; y = e-a;
r = Math.sqrt(x*x+y*y)
if( r > 0) { x /= r; y /= r; }
else {
x = e-d; y = c;
r = sqrt(x*x+y*y)
if( r > 0) { x /= r; y /= r; }
else {
x = 1; y = 0;
}
}
System.out.println("Eigenvector1: (" + x + "," + y + ")");
e = eigenvalue2;
x = b; y = e-a;
r = Math.sqrt(x*x+y*y)
if( r > 0) { x /= r; y /= r; }
else {
x = e-d; y = c;
r = sqrt(x*x+y*y)
if( r > 0) { x /= r; y /= r; }
else {
x = 0; y = 1;
}
}
System.out.println("Eigenvector2: (" + x + "," + y + ")");
According to your edit comments, I think this revised code should produce what you want. I've removed your temp variables and I'm returning your computed basis values as a 2 element array. Excuse me if I've misnamed anything, my maths is a little rusty.
public static void main(String[] args) {
// the input matrix
double[][] matrix = {
{1.0, 1.0},
{1.0, -1.0}
};
// compute the basis
double[] basis = getBasis(matrix);
System.out.println("Basis: (" + basis[0] + ", " + basis[1] + ")");
}
public double[] getBasis(double[][] matrix){
double a = matrix[0][0];
double b = matrix[0][1];
double c = matrix[1][0];
double d = matrix[1][1];
double eigenvalue1 = ((a+d) + Math.sqrt( Math.pow(a-d,2) + 4*b*c))/2;
double eigenvalue2 = ((a+d) - Math.sqrt( Math.pow(a-d,2) + 4*b*c))/2;
// store the basis in a 2 element array
double[] basis = new double[2];
for (double y = -1000; y <= 1000; y++) {
for (double x = -1000; x <= 1000; x++) {
if (((a-eigenvalue1)*x + b*y == 0) && (c*x + (d-eigenvalue1)*y == 0)) {
System.out.println("Eigenvector1: (" + x + "," + y + ")");
basis[0] = eigenvalue1;
}
}
}
for (double y = -10; y <= 10; y++) {
for (double x = -10; x <= 10; x++) {
if (((a-eigenvalue2)*x + b*y == 0) && (c*x + (d-eigenvalue2)*y == 0)) {
System.out.println("Eigenvector2: (" + x + "," + y + ")");
basis[1] = eigenvalue2;
}
}
}
return basis;
}
Output:
Basis: (1.4142135623730951, -1.4142135623730951)
I'm attempting to fill a 2D array with the numbers 1 to 1000 and then show all the factors of those numbers. I then need to find all the prime numbers in the same array and output them. Here's what I have so far, keep in mind that I was hoping to do every step in its own method then return them but have not got that far yet
int i = 0;
//int x = 0;
String primeNumber = "";
int[] [] factorArray = new int [1000] [];
for (int x = 0 ; x < 1000 ; x++)
{
int remainder;
int y;
remainder = x % 2;
y = x / 2;
if (remainder != 0)
System.out.println (x + ": " + "1, " + x);
else if (remainder == 0)
System.out.println (x + ": " + (y) + " , " + (y / 2) + " , " + " 1, " + x);
}
for (i = 1 ; i <= 1000 ; i++)
{
int ctr = 0;
for (int x = i ; x >= 1 ; x--)
{
if (i % x == 0){
ctr = ctr + 1;
}
}
if (ctr == 2)
{
primeNumber = primeNumber + i + " ";
}
}
System.out.print ("Prime numbers from 1 - 1000 are : \n" + primeNumber);
I have a question on the method and process of how to look at these generated arrays.
Basically I want to create an array of [a,b,c,(a+b+c)] and as well as a second array of [d,e,f,(d+e+f)] and if the third element in array1 and array2 are the same, display the arrays to strings.
int num = 10;
for(int a = 0; a < num; a++){
for(int b = 0; b < num; b++){
for(int c = 0; c < num; c++){
if(a<=b && b<=c){
arrayOne[0] = a;
arrayOne[1] = b;
arrayOne[2] = c;
arrayOne[3] = (a+b+c);
}
}
}
}
for(int d = 0; d < num; e++){
for(int e = 0; e < num; e++){
for(int f = 0; f < num; f++){
if(d<=e && e<=f){
arrayTwo[0] = d;
arrayTwo[1] = e;
arrayTwo[2] = f;
arrayTwo[3] = (f -(d+e));
}
}
}
}
as you can see I am beyond stump.I am not quite sure where i can get each iteration of the arrays and compare the values by matching the sums in each array and as well as displaying the respective array they are in. Thank you all in advanced.
If I understand your question correctly if a=1, b=3, c=4 and d=2, e=3, f=3 you'd like to print something along the lines of 1 + 3 + 4 = 8 = 2 + 3 + 3. First, what you're doing right now is creating two arrays like Floris described in the comment. What you want to do is store all the values in one array of arrays, as follows:
int max; \\ To determine the value of max see the edit below.
int array[][] = new int[max][num];
int index = 0;
for (int a=0; a < num; a++) {
for (int b=a; b < num; b++) {
for (int c=b; c < num; c++) {
array[index][0] = a;
array[index][1] = b;
array[index][2] = c;
array[index][3] = a + b + c;
index++;
}
}
}
for (int i = 0; i < max; i++) {
for (int j = i; j < max; j++) {
if (array[i][3] == array[j][3]) {
string outString = array[i][0] + " + " + array[i][1] + " + " + array[i][2] + " = " + array[i][3] + " = " + array[j][0] + " + " + array[j][1] + " + " + array[i][2];
System.out.println(outString);
}
}
}
You can see that I improved performance by starting b from a and c from b since you are throw out all the values where b < a or c < b. This also should eliminate the need for your if statement (I say should only because I haven't tested this). I needed to use an independent index due to the complexities of the triple nested loop.
Edit 2: Ignore me. I did the combinatorics wrong. Let An,k be the number of unordered sets of length k having elements in [n] (this will achieve what you desire). Then An,k = An-1,k + An,k-1. We know that An,1 = n (since the values are 0, 1, 2, 3, 4, ..., n), and A1,n = 1 (since the only value can be 11111...1 n times). In this case we are interested in n= num and k = 3 , so plugging in the values we get
A_num,3 = A_num-1,3 + A_num,2
Apply the equation recursively until you come to an answer. For example, if num is 5:
A_5,3 = A_4,3 + A_5,2
= A_3,3 + A_4,2 + A_4,2 + A_5,1
= A_3,3 + 2(A_4,2) + 5
= A_2,3 + A_3,2 + 2(A_3,2) + 2(A_4,1) + 5
= A_2,3 + 3(A_3,2) + 2(4) + 5
= A_1,3 + A_2,2 + 3(A_2,2) + 3(A_3,1) + 2(4) + 5
= 1 + 4(A_2,2) + 3(3) + 2(4) + 5
= 1 + 4(A_1,2) + 4(A_2,1) + 3(3) + 2(4) + 5
= 1 + 4(1) + 4(2) + 3(3) + 2(4) + 5
= 5(1) + 4(2) + 3(3) + 2(4) + 5
It looks like this may simplify to (num + (num - 1)(2) + (num - 2)(3) + ... + (2)(num - 1) + num) which is binomial(num, num) but I haven't done the work to say for sure.
int givenNumber = 10;
int []arrayOne = new int [4];
int []arrayTwo = new int [4];
int count = 0;
for ( int i = 0; i < givenNumber; i ++)
{
for ( int x = 0; x < givenNumber; x ++ )
{
for ( int a = 0; a < givenNumber; a++ ){
arrayOne[0] = (int)(a * java.lang.Math.random() + x);
arrayOne[1] = (int)(a * java.lang.Math.random() + x);
arrayOne[2] = (int)(a * java.lang.Math.random() + x);
arrayOne[3] = (int)(arrayOne[0]+arrayOne[1]+arrayOne[2]);
}
for ( int b = 0; b < givenNumber; b++ ){
arrayTwo[0] = (int)(b * java.lang.Math.random() + x);
arrayTwo[1] = (int)(b * java.lang.Math.random() + x);
arrayTwo[2] = (int)(b * java.lang.Math.random() + x);
arrayTwo[3] = (int)(arrayTwo[0]+arrayTwo[1]+arrayTwo[2]);
}
if (arrayOne[3] == arrayTwo[3])
{
for ( int a = 0; a < 2; a++ )
{
System.out.print(arrayOne[a] + " + ");
} System.out.print(arrayOne[2] + " = " + arrayOne[3] + " = ");
for ( int a = 0; a < 2; a++ )
{
System.out.print(arrayTwo[a] + " + ");
} System.out.print(arrayTwo[2]);
System.out.println("\n");
count += 1;
}
}
}
if (count == 0)
System.out.println(
"\nOops! you dont have a match...\n" +
"Please try running the program again.\n");
I'm practicing writing for loops in Java. This is what I have so far:
int a = 0;
int b = 0;
int c = 0;
int d = 0;
System.out.println ("Num "+"Square "+"Num "+"Square "+"Num "+"Square "+"Num "+"Square ");
for (int x = 1; x<=20; x++) {
a = x * 1;
b = 20+a;
c = 40+a;
d = 60+a;
for (int y = 1; y <= 1; y++) {
System.out.println (a + " " + (a * a) + " " + b + " " + (b * b) + " " + c +
" " + (c * c) + " " + d + " " + (d * d));
}
}
I don't want to change the code at all (meaning I don't want to add anything more complicated than I already have), but I'd like the columns to line up. When the numbers start increasing in digits, obviously the spacing gets messed up. Any ideas on how to fix this?
EDIT: To answer your questions, I'm pretty new to this and haven't taught myself much more than this. I'm more than sure there are way better ways to do this, but I'm just practicing my for loops. Anywho, thanks for the help.
You can use tab(\t) to format it. A double tab will provide more space in your case.
// Changed Num to Number - Out of habit
System.out.println("Number\t\t" + "Square\t\t" + "Number\t\t" + "Square\t\t" + "Number\t\t" + "Square\t\t" + "Number\t\t" + "Square");
System.out.println(a + "\t\t" + (a * a) + "\t\t" + b + "\t\t" + (b * b) + "\t\t" + c + "\t\t" + (c * c) + "\t\t" + d + "\t\t" + (d * d));
public String appendBlankSpace(int val){
String returnValue=Integer.toString(val);
int lengthTobeAppend=10-returnValue.length();
//Here you can use any number instead of 10.It is for space allocated for a column
for(int i=1;i<=lengthTobeAppend;i++){
returnValue= " " + returnValue;
}
return returnValue;
}
now you can use,
System.out.println (appendBlankSpace(a) + appendBlankSpace(a * a) + appendBlankSpace(b) + appendBlankSpace(b * b) + appendBlankSpace(c) + appendBlankSpace(c * c) + appendBlankSpace(d) + appendBlankSpace(d * d));
I hope it will help you.
you can store a,b,c,d values in array like inside loop
for (int x = 1; x<=20; x++) {
a = x * 1;
b = 20+a;
c = 40+a;
d = 60+a;
int [] array = {a,b,c,d}
//using enhanced for loop
for(int ele : array){
System.out.println(ele +"\t\t"+ ele*ele)
}
}
output will appear like :
2 4
3 9
4 16
Use printf.
public class Squarely {
public static void main(String[] args) {
for (int x = 1; x <= 20; ++x) {
int a = x, b = 20+x, c = 40+a, d = 60+a;
System.out.printf("%6d %6d %6d %6d %6d %6d %6d %6d%n",
a, a*a, b, b*b, c, c*c, d, d*d);
}
}
}
Now I have completed the finding 23 sets of x y z values satisfy the condition x^3+y^3=1+z^3 & x
int setsFound = 0;
System.out.println("The first 23 sets ordered by increasing x.");
for (long x = 1; setsFound < 23; x++) {
for (long z = x + 1; z<x*x; z++) {
long y = (long) Math.pow((1 + z*z*z - x*x*x), 1f/3f);
if (x * x * x == 1 + z * z * z - y * y *y && x<y && y<z) {
setsFound++;
System.out.println("X: " + x + ", Y: " + y + ", Z: " + z);
}
}
}
But the code I have is very inefficient, can anyone help me to fix this please?
Here is a working code:
int setsFound = 0;
System.out.println("The first 23 sets ordered by increasing x.");
for (long z = 1; setsFound < 23; z++) {
for (long y = z - 1; y > 0; y--) {
long x = (long) Math.pow((1 + z * z * z - y * y * y), 1f/3f);
if(y <= x) break;
if (x * x * x == 1 + z * z * z - y * y *y) {
setsFound++;
System.out.println("X: " + x + ", Y: " + y + ", Z: " + z);
}
}
}
Couple of problems in the old one:
1/3 == 0 (because it's integer division) //use 1f/3f
x and z are swapped - you want z > x, not the other way around
(long)Math.pow(4*4*4, 1.0/3) == (long)3.9999999999999996 == 3 // use round
If you start the other way, with X < Y < Z by incrementing Y and Z up to a limit, you can gain some efficiencies. Once Z^3 > X^3 + Y^3 + 1, you can skip to the next Y value due to the concavity of the cubic function.
This implementation in C# works pretty fast on a laptop:
UInt64 setsFound = 0;
UInt64 xlim = 10000;
UInt64 ylim = 1000000;
UInt64 zlim = 10000000;
//int ctr = 0;
Console.WriteLine("The first 23 sets ordered by increasing x.");
Parallel.For(1, (long)xlim, new ParallelOptions { MaxDegreeOfParallelism = 4 }, i =>
//for (UInt64 i = 0; i < xlim; i++)
{
UInt64 x = (UInt64)i;
UInt64 xCu = x * x * x;
int zFails = 0;
for (UInt64 y = x + 1; y < ylim; y++)
{
UInt64 yCu = y * y * y;
zFails = 0;
for (UInt64 z = y + 1; z < zlim & zFails < 1; z++)
{
UInt64 zCu = z * z * z;
if (xCu + yCu - zCu == 1)
{
Console.WriteLine(String.Format("{0}: {1}^3 + {2}^3 - {3}^3 = 1", setsFound, x, y, z));
setsFound++;
}
else if (zCu > xCu + yCu - 1)
{
zFails++;
}
}
}
}
);
Obviously you can take out the parallelization. Also, here are the first 19 elements in that set (computer is still running, I'll try to post the last 4 later):
output http://desmond.yfrog.com/Himg640/scaled.php?tn=0&server=640&filename=8qzi.png&xsize=640&ysize=640