The exercise asks me to: enter a quantity x of natural numbers greater than one and show on the screen the decomposition into prime factors of each one, indicate which of them has more prime factors, e.g.:
72 : 2 - 2 - 2 - 3 - 3
50 : 2 - 5 - 5
type
vec=array [0..100] of byte;
var
n,r:byte;
v,m: vec;
procedure leevec(var n : byte; var v: vec);
var
i: byte ;
begin
write('ingre la cantidad de elementos ');
readln(n);
for i:=1 to n do
begin
write(i,': ');
readln(v[i]);
end;
end;
procedure factorprimo(n: byte; var r : byte ;v: vec; var t: vec);
var
i,d:byte;
begin
for i :=1 to n do
r:=1;
while v[i] > 1 do
begin
d:=2;
while (v[i] mod d <> 0) do
d:=d+1; // son los factores primos
t[r]:=d;
r:=r+1; // contador
v[i]:=v[i] div d;
end;
end;
procedure escritura(k:byte; a:vec);
var
i:byte;
begin
for i:=1 to k do
begin
write(a[i],' ');
end;
writeln;
end;
begin
leevec(n,v);
factorprimo(n,r,v,m);
escritura(r-1,v);
end.
This is what I was able to do but as a result I get the prime factors of the last element of the vector and I still couldn't count who has the most FP.
Welcome to StackOverlow in Spanish.
The first recommendation that reading your program would give you is that you use variables (as you have done with methods) with descriptive names.
n, r, v, t, d,...
they do not explain much and can lead to confusion.That brings me to the first problem (which has to do with this). You make a call at the end of the method:
Where the second parameter is the vector to write. You pass one as a parameter
v
(which is the input vector) and effectively the program is writing the values of the input vector.To an entry like this:
With 2 values,
55 y 66
you are returning them on the output (the index doesn't quite work)The second recommendation is that you try to divide the problem into smaller parts that are easier for you to program and check.
For example, in the method
factorprimo
, you try to generate the factors of all the input numbers (the vectorv
). The most logical thing would be to make a procedure that calculates the factors of a single number and then make the call with a loop as many times as you need.Because of this comes your second problem. This procedure
factorprimo
is using the same output vector for the factors of all numbers. Therefore, it only stores the factors of the last one. Let's say you calculate the factors ofv[1]
and store them in thevector t
, andv[2]
to store it back in thevector t
(mashing the first ones).You can modify your initial message, so I would try to review these things and change the code so that I can continue reviewing.
I think you are not going wrong, my first recommendation is to use descriptive identifiers, both in the names of types and variables, as well as in functions, procedures; finally, in everything.
Then, to maintain a standard, in the definition of the type
Vec
the array goes from 0 to 255, and in the routine that reads it, you start asking at index 1. Although this does not cause a failure, it does create confusion. As a general rule, in Pascal, static array indices are 1-based. If you don't have a good reason to do otherwise, sticking to that convention will keep your code consistent.Putting these two things together in code, we would be left with something like this:
Then, perhaps because you're going too fast and trying to solve the whole problem at once, I'd say you're mixing up your ideas. I consider it advisable to create a function that finds and counts the factors of a single number, and then pulls it to be able to answer the problem posed.
If you think about it, once you have this function, what you would need is to go through the vector, finding out the number of factors that each number has and, at each step of the iteration, see if that number is greater than the maximum recorded up to that point. If it is, record that value at most, remember the number associated with it, and so on until the end of the array is reached.
By then, we will have in memory the number and quantity of factors that correspond to the one that has the most, it would be just a matter of printing it.
Making the function
EncontrarFactores
print the factors of each number and return the count of them, the main program could look something like this:Which would produce output like this (in my test run):