Shouldn't your long
be an int
? Java has a tendency to complain about number type mismatches. Oh, and most recursive problems usually have an initial pattern specified, that sometimes has to be taken care of as a special case in code.
Works, this is how I have written the exercise, I have to do the same. I am using Dr Java at no time did he warn me.
But apparently if I declare a variable and don't initialize it, if I get error
Java:
int i;
//Apparently he does not like that I declare a variable and do not initialize it with a value.
//It would give me an error
int i = 0;
//This is what you want to see, that it gives it a value, but in theory when declaring a variable its value is already zero, why would it have to initialize at 0?
//I don't know about Java or Dr java, I still don't understand ...
Guys, I've been thinking about a count digit in ASM, but it seems it's being harder for me than I thought.
I have thought of using
bsr and
bsf, example:
Code:
.section .text
.globl _start
_start:
movq $0x8000000000000010, %rbx
bsfq %rbx, %rdi // rdi = 4 index bit
bsrq %rbx, %rsi // rsi = 63 index bit
I have been thinking about knowing where the bits of the number end, for example if bsr gives me the index 31 0 30 I know that the number is 10 digits since the maximum number of an unsigned int is 4294967295
But of course, calculating the 10,1000,10000 is more complicated, it can be done but it is more complicated than it seems.
I am still thinking about my fibonacci exercise, I do not pretend that my exercises do much less but I do not understand this, the explanation by my teacher is practically zero.
He does not say how he wants the codes and of course, you have to interpret what he wants to see.
/* Write the first 100 numbers of the Fibonacci serie
* @param n the number of numbers of the serie of Fibonacci
* 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, ... */
public void writeFibonacci(long n)
That's all I have, the problem I see is that being void I will have to use a variable, right?
Global, I will have to use n as a counter until reaching 100 numbers, I really don't understand why it puts n if they are the first 100 numbers, the value is fixed, for a variable n? It could be used to increase the numbers to be displayed or to minimize them.
What would be the path to take? As I said, I do not pretend that the tasks give me, but it is what occurs to me to create two global variables with the two default numbers BigDecimal.ONE and ZERO and from there show the numbers and add the sequence and save them in those variables, as I have in my iterative code.
But of course if I want to call the function again in the main, after seeing the function called, I have to initialize the variables to 0 and 1 again, since they will be written with the values from before.
These are all the functions that I had to create, in my exercises, apparently they all work well, ask a colleague and apparently they don't need to work with zero and negative numbers, anyway
Java:
public static int countDigitsRecursiva(long num) {
if(num > -10 && num < 10) return 1;
else return 1 + countDigitsRecursiva(num / 10);
}
public static int ocurrencesRecursiva(long num, int digitc, int c)
{
if (num == 0 && c == 0) return 1;
else if (num == 0) return 0;
if (num % 10 == digitc) return 1 + ocurrencesRecursiva(num / 10, digitc, c + 1);
return ocurrencesRecursiva(num / 10, digitc, c + 1);
}
public static double power10Recursive(int exp) {
if (exp == 0) return 1;
if (exp < 0) return 1 / (10 * power10Recursive(-(exp + 1)));
else return 10 * power10Recursive(exp - 1);
}
public static double powerRecursive(double base, int exp) {
if (exp == 0) return 1;
if (exp < 0) return 1 / (base * powerRecursive(base, Math.abs(exp + 1)));
else return (base * powerRecursive(base, exp - 1));
}
Thanks guys.