Hello i got one task to do. output should be like this:
{
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81
}
I tried to do it: here is my code:
public class ContnueUzOznakoMojNacin
{
public static void main(String args[])
{
int k=0, v=0;
int j;
for(int i=0;i<10;i++)
{
for(j=10-i;j<10;j++)
{
System.out.print(v+" ");
v+=k;
}
System.out.println();
v=0;
k++;
}
}
}
And the output i get is wrong and i dont get it why.
here it is:
{
0
0 2
0 3 6
0 4 8 12
0 5 10 15 20
0 6 12 18 24 30
0 7 14 21 28 35 42
0 8 16 24 32 40 48 56
0 9 18 27 36 45 54 63 72
}
When i follow these loops and increments from my program i cant find mistake.
first line is ok output should be 0;
but second line, output should be 0 1; not 0 2 ?
I dont need you to give me code for this task i need you to help me to do it, to tell me where i made mistake so i do it on my own. Thanks :)
Change this line:
for(j=10-i;j<10;j++)
to this:
for(j=9-i;j<10;j++)
And here's an explanation:
So i starts at 0, right? What will be i's maximum? 9, because in for(int i=0;i<10;i++), i cannot get to 10.
Let's look at how that affects for(j=10-i;j<10;j++). If i is 9 (the last row), then the j loop will only run 9 times. j will equal 1,2,3,4,5,6,7,8,9. That's only 9 loops. If you look at the bottom of the upper triangle, you can see that 0 9 18 27 36 45 54 63 72 has only 9 numbers.
We want j to run 10 times, as you can see by the base of the correct triangle: 0 9 18 27 36 45 54 63 72 81. How do we do this? We make j run one more time on every i loop, by decreasing the starting number (10-i) by one (which equals 9-i). That is how you arrive at
for(j=9-i;j<10;j++)
Related
I am trying to output 12 times tables. Where it will go through all the times tables from 1 to 12. And each times table will go up to *12. For examples, the output should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
...
12 24 36 48 60 72 84 96 108 120 132 144
Here is the code I have so far, I seem to be very close. But I am struggling with perfecting it.
public class Tables {
public void generateTable()
{
//Put the code for your times-table here
int i;
int j;
for(i=1; i<=12; i++)
{
for(j=i; j<=i*12; j = j+1)
{
System.out.print(j*i + " ");
}
System.out.println();
}
}
public static void main(String args[])
{
Tables t = new Tables();
t.generateTable();
}
}
I am receiving the following output for this code:
1 2 3 4 5 6 7 8 9 10 11 12
4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48
9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108
...
In the inner loop, you only want to go from 1 to 12 instead of from i to i*12.
You could also declare i and j more local in the respective loop where the initialization is done.
I have a data file which consist of Marks of two subjects.
The file look like that
Sl_No: Marks1 Marks2
1 10 20
2 10 20
3 10 20
4 10 20
5 10 10
6 20 10
7 20 10
8 20 10
9 20 30
10 20 22
11 21 22
12 21 22
13 21 23
14 10 20
15 10 20
Now my objective is that whenever it get same pair of value add this marks. So as per my example sl_no 1 to 4 have same pairs of values (10,20). So it return 40 for Marks1 and 80 for marks2. Sl_No: 5 doesnot match with sl_no: 4 so it is remain same. Serial no: 6 to 8 have a matching pair so it return 60 and 30. Sl_no: 9.0 and 10.0 has no match so it remain same as it is. Sl_no: 11 and 12 had a match so return 42 and 44.and sl_no: 14 and 15 had a match so return 20 and 40.
Desired output:
4 40 80
5 10 10
8 60 30
9 20 30
10 20 22
12 42 44
13 21 23
15 20 40
My Code:
public class Marksmatch {
public static void main(String args[]){
int []sl_no={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int []Marks1={10,10,10,10,10,20,20,20,20,20,21,21,21,10,10};
int []Marks2={20,20,20,20,10,10,10,10,30,22,22,22,23,20,20};
int addMarks1=0,addMarks2=0;
for(int i=0;i<sl_no.length-1;i++){
if(Marks1[i]== Marks1[i+1]&&Marks2[i]==Marks2[i+1]){
addMarks1=addMarks1+Marks1[i+1];
addMarks2=addMarks2+Marks2[i+1];
System.out.println(addMarks1);
System.out.println(addMarks2);
}
}
}
}
Output:
10
20
20
40
30
60
50
70
70
80
91
102
101
122
It did not give me the desire output. How could I proceed?
Your logic is wrong. You should keep track of the sums of the current group, and only print them once you are done with the group:
int addMarks1=Marks1[0],addMarks2=Marks2[0]; // initialize the sums of the first group
for(int i=0;i<sl_no.length-1;i++){
if(Marks1[i]== Marks1[i+1] && Marks2[i]==Marks2[i+1]) {
// add to the current group
addMarks1=addMarks1+Marks1[i+1];
addMarks2=addMarks2+Marks2[i+1];
} else {
// print the previous group and start a new group
System.out.println(sl_no[i] + " " + addMarks1 + " " + addMarks2);
addMarks1=Marks1[i+1];
addMarks2=Marks2[i+1];
}
}
// print the last group
System.out.println(sl_no[sl_no.length-1] + " " + addMarks1 + " " + addMarks2)
Output:
4 40 80
5 10 10
8 60 30
9 20 30
10 20 22
12 42 44
13 21 23
15 20 40
The problem:
I want all numbers from 1-50 that are not divisible by 7, but also don't have a 7 in them, like 17,27,etc. The code below works but (i-10)%7 has to start from i=6. Now it thinks the number 3 doesn't count cause (3-10)=-7 that is 0 mod 7. how do i solve this in the if statement?
for(int i=1; i<=50;i++){
if(i%7!=0 && (i-10)%7!=0){
System.out.println(i);
Your second condition is wrong. 27 ends in 7, but (27-10)%7!=0.
You just need to check that i % 10 is not 7.
if(i%7!=0 && i%10!=7)
This will do the trick
for(int i=1; i<=50;i++){
if(i==7 || i==3)
{
if(i==3)
System.out.println(i);
}
else if(i%7!=0 && (i%10)%7!=0){
System.out.println(i);}
}
output is...
1 2 3 4 5 6 8 9 11 12 13 15 16 18 19 22 23 24 25 26 29 31 32 33 34 36 38 39 41 43 44 45 46 48
UPDATE
If you want your if condition to start checking from i=6 then you can do this...
for(int i=1; i<=50;i++){
if(i>=6 && i%7!=0 && (i%10)%7!=0)
System.out.println(i);
}
but the output will change to...
6 8 9 11 12 13 15 16 18 19 22 23 24 25 26 29 31 32 33 34 36 38 39 41 43 44 45 46 48
This should work. Casting it to a string and just checking if it contains the string "7".
if(i % 7 != 0 && ((String)i).contains("7"))
Its seems to basic:
find if the number has 7 or not : number%10==7
find if the number is divisible by 7 or not : number%7==0
combine both in Or clause
if (number%10==7||number%7==0)
do watever.......
but it only applicable if the number<50
If you're checking if each number contains a 7 instead of '(i-10)%7!=0' I would recommend casting i to a string (i + "") and then checking if it contains the char 7
using only i%10 !=7 will only check to see if the last digit is not 7.
Have a problem with my output when creating a multiplication table 1-12 inclusive. below is my code and output
public class Multiplication
{
public static void main(String[]args)
{
int row,column;
System.out.println("Below is a multiplication table from 1-12 inclusive");
for (row=1;row<=12;row++)
{
System.out.printf("%4d\n",row);
for (column=1;column<=12;column++)
System.out.printf("%6d",row*column);
}
}
}
and my output is
Below is a multiplication table from 1-12 inclusive
1
1 2 3 4 5 6 7 8 9 10 11 12 2
2 4 6 8 10 12 14 16 18 20 22 24 3
3 6 9 12 15 18 21 24 27 30 33 36 4
4 8 12 16 20 24 28 32 36 40 44 48 5
5 10 15 20 25 30 35 40 45 50 55 60 6
6 12 18 24 30 36 42 48 54 60 66 72 7
7 14 21 28 35 42 49 56 63 70 77 84 8
8 16 24 32 40 48 56 64 72 80 88 96 9
9 18 27 36 45 54 63 72 81 90 99 108 10
10 20 30 40 50 60 70 80 90 100 110 120 11
11 22 33 44 55 66 77 88 99 110 121 132 12
12 24 36 48 60 72 84 96 108 120 132 144
My problem is in getting the far right column to be on the far left. I've tried researching why only the number '1' appears and then it jumps but cant find anything
Your problem is the first of your two printf statements. You should just be able to replace it with System.out.printf("\n");.
(The "jump" to which you refer is a newline, which is represented by the \n character.)
Try this:
public class Multiplication
{
public static void main(String[]args)
{
int row,column;
System.out.println("Below is a multiplication table from 1-12 inclusive");
for (row=1;row<=12;row++)
{
System.out.printf("%4d",row);
for (column=1;column<=12;column++)
System.out.printf("%6d",row*column);
System.out.printf("\n");
}
}
}
for (row=1;row<=12;row++)
{
System.out.printf("%4d",row);
for (column=1;column<=12;column++)
System.out.printf("%6d",row*column);
if(column==12) System.out.printf("\n");
}
I was reading about the DP version of fibonnaci.
In Sedgewick I saw:
int[] T = new int[47]; for storage of the previous calculations. Elsewhere I saw that the max input for fibonacci should be less than 92.
It is not clear to me how does these numbers come up? I understand that it has to do with overflow and size of int but I am not clear how we end up with these limits.
Any help?
There is a closed-form expression for the n-th Fibonacci number, Binet's formula,
F(n) = (φ^n - ψ^n) / (φ - ψ)
where
φ = (1 + √5)/2; ψ = 1 - φ = -1/φ
Now |ψ| < 1, so the ψ^n term converges to 0 pretty fast, hence in estimating the size of F(n) it can be ignored except for the first few numbers.
So if you have an integer type with b bits used for the representation of positive integers, you can represent the Fibonacci numbers with
F(n) < 2^b
(since the maximal number that can be represented is 2^b - 1). Ignoring the ψ^n term and using φ - ψ = √5, we find the condition
φ^n < 2^b * √5
<=> n*log φ < b*log 2 + 1/2*log 5
<=> n < b*(log 2 / log φ) + 1/2*(log 5 / log φ)
log 2 / log φ ≈ 1.44042009 and 1/2*(log 5 / log φ) ≈ 1.672275938, so with a signed 32-bit integer type (which has 31 bits to represent positive numbers, since one bit is used for the sign), you can represent the Fibonacci numbers for
n < 31*(log 2 / log φ) + 1/2*(log 5 / log φ) ≈ 44.65 + 1.67 ≈ 46.32
i.e. the 47 Fibonacci numbers with index between 0 and 46 (inclusive). With an unsigned 32-bit integer type you could also represent F(47).
With a signed 64-bit integer type, you can represent the Fibonacci numbers for
n < 63*(log 2 / log φ) + 1/2*(log 5 / log φ) ≈ 90.75 + 1.67 ≈ 92.42
and with an unsigned 64-bit integer type You can also represent F(93).
Well, the fibonacci series grows (approximately) exponentially with a ratio of 1.618 (the golden ratio).
If you take the log base 1.618 of Integer.MAX_VALUE it will therefore tell you approximately how many iterations you can go before overflowing....
Alternatively, you can determine empirically when it overflows just by doing the calculations....
(signed) int has a value range of −2.147.483.648 ... 2.147.483.647, so storing a fibonanacci number larger than 2.147.483.647 does not work.
The question now is: What is the first fibonnacci number larger than that value?
Spreadsheet says:
n fib(n)
1 0
2 1
3 1
4 2
5 3
6 5
7 8
8 13
9 21
10 34
11 55
12 89
13 144
14 233
15 377
16 610
17 987
18 1597
19 2584
20 4181
21 6765
22 10946
23 17711
24 28657
25 46368
26 75025
27 121393
28 196418
29 317811
30 514229
31 832040
32 1346269
33 2178309
34 3524578
35 5702887
36 9227465
37 14930352
38 24157817
39 39088169
40 63245986
41 102334155
42 165580141
43 267914296
44 433494437
45 701408733
46 1134903170
47 1836311903
48 2971215073
49 4807526976
So you can see: fibonnacci numbers after #47 won't fit in a (signed) int.
To clarify: Unlike C Java does not have unsigned types. So the emphasis on signed int is kind of obsolete.
You may use following formula:
F(2n) = F(n)* (2*F(n-1) + F(n))
n=46
F(92) = F(46) * (2*F(45) +F(46))
This is a Matrix Form of Fibonacci.
Full list of number (ulong not overflowed)
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55
11 89
12 144
13 233
14 377
15 610
16 987
17 1597
18 2584
19 4181
20 6765
21 10946
22 17711
23 28657
24 46368
25 75025
26 121393
27 196418
28 317811
29 514229
30 832040
31 1346269
32 2178309
33 3524578
34 5702887
35 9227465
36 14930352
37 24157817
38 39088169
39 63245986
40 102334155
41 165580141
42 267914296
43 433494437
44 701408733
45 1134903170
46 1836311903
47 2971215073
48 4807526976
49 7778742049
50 12586269025
51 20365011074
52 32951280099
53 53316291173
54 86267571272
55 139583862445
56 225851433717
57 365435296162
58 591286729879
59 956722026041
60 1548008755920
61 2504730781961
62 4052739537881
63 6557470319842
64 10610209857723
65 17167680177565
66 27777890035288
67 44945570212853
68 72723460248141
69 117669030460994
70 190392490709135
71 308061521170129
72 498454011879264
73 806515533049393
74 1304969544928657
75 2111485077978050
76 3416454622906707
77 5527939700884757
78 8944394323791464
79 14472334024676221
80 23416728348467685
81 37889062373143906
82 61305790721611591
83 99194853094755497
84 160500643816367088
85 259695496911122585
86 420196140727489673
87 679891637638612258
88 1100087778366101931
89 1779979416004714189
90 2880067194370816120
91 4660046610375530309
92 7540113804746346429
As we see
45 1134903170
46 1836311903
92 7540113804746346429
7540113804746346429 = 1836311903*(2*1134903170 + 1836311903)
First, is already done some classes for representing large numbers beyond the limits for some primitive type, like with some C modifier of the type long long. To see the largest term look on largest known Fibonacci number.