I am trying to factor a given number a, so I have written the following Pascal code:
program prime_factors;
var b:array[1..1000] of integer;
k,i,d,a:integer;
begin
k:=0;
write(' enter number ');
read(a);
while a>1 do
if a mod d =0 then
begin
k:=k+1;
b[k]:=d;
a:=a div d;
end
else
if d=2 then
d:=d+1
else
d:=d+2;
for i:=1 to k do
write(b[i],' ');
readln();
readln();
end.
But when I run it, it gives me error 200 or runtime error, but I can't determine what is problem. I have used k as length of numbers of factors in b array. Should I think what problem is with index k?
you should define D:=2; right after the Begin :D because it's default value is 0.
Related
Here are the rules of a simple game:
2 players a and b hide their right hands behind their backs. Each chooses to hold a certain number of fingers from 0 to 5 always behind their backs. Both players show their hands at the same time. If the sum of the numbers of fingers shown is a pair, player a wins, otherwise player b wins. The problem is determining the winner by the computer
I've tried this programming in Turbo Pascal but they keep showing the ":" expected but ";" found or the "operand type doesn't match operator"
Program parity;
var nbr1;nbr2,remainder:integer;
begin
read(nbr1,nbr2);
tot:=nbr1+nbr2;
remainder:=tot mod 2;
if remainder=0 then begin write('winner is A')
end
else write('winner is B');
end.
First, let's clean up your code formatting.
program parity;
var
nbr1; nbr2, remainder : integer;
begin
read(nbr1, nbr2);
tot := nbr1 + nbr2;
remainder := tot mod 2;
if remainder = 0 then
begin
write('winner is A')
end
else
write('winner is B');
end.
Your line nbr1; nbr2, remainder : integer; has an extra terminator so your compiler sees it as thought you'd written:
nbr1;
nbr2, remainder : integer;
It thinks you're missing a type on the declaration of nbr1, but I strongly suggest you just typoed when writing: nbr1, nbr2, remainder : integer;
I want to calculate the sum of the digits of a user-specified number first. Then I want to check if this number is divisible by the sum of its digits. Unfortunately, for example, for 21 it shows that it is not divisible, and for 200 that it is divisible. Maybe someone helps me. I'm just learning the language pl SQL.
DECLARE
n number(5):=&give_number;
temp_sum INTEGER;
r INTEGER;
a varchar(20);
BEGIN
temp_sum := 0;
WHILE n <> 0 LOOP
r := MOD(n, 10);
temp_sum := temp_sum + r;
n := Trunc(n / 10);
END LOOP;
a:=mod(r,temp_sum);
if a = 0 then
dbms_output.put_line('Divisible');
else
dbms_output.put_line('No divisible');
end if;
END;
You are checking if r is divisible by temp_sum and, if you consider what values they are holding then r is the most-significant digit and temp_sum is the digit sum. Which for an input of 21 checks whether 2 (the most-significant digit) is exactly divisible by 3, which it correctly reports that it is not divisible.
To fix it, you want to keep the input number in a variable that you do not modify and use that in the final comparison and not r:
If we remove some of the unnecessary variables and give the others more meaningful names:
DECLARE
input_value NUMBER(5) := &give_number;
remainder_value NUMBER(5) := input_value;
digit_sum INTEGER;
BEGIN
digit_sum := 0;
WHILE remainder_value <> 0 LOOP
digit_sum := digit_sum + MOD(remainder_value, 10);
remainder_value := TRUNC(remainder_value / 10);
END LOOP;
dbms_output.Put_line(remainder_value);
dbms_output.Put_line('sum of digits = ' || digit_sum);
IF MOD(input_value,digit_sum) = 0 then
dbms_output.put_line('Divisible');
ELSE
dbms_output.put_line('Not divisible');
END IF;
END;
/
Then when you put in 21 the output is:
0
sum of digits = 3
Divisible
And for 202, the output is:
0
sum of digits = 4
Not Divisible
db<>fiddle here
Here is a more compact way to write your PL/SQL block, using some of the features specific to the language. Perhaps the most interesting one is the nesting of program units (subroutines) - something that, in C for example, is not permitted. I am referring to the declaration and full code of a "helper function" (the sum of digits function) right in the DECLARE section of the outer block.
The function also demonstrates a compact way to write the function recursively. In this case recursion isn't too deep (only as many recursive calls as there are digits in the input number); in general, if you can write the same function using a loop, as you did, instead of using recursion, is preferred - far less overhead. On the other hand, with the proper settings (an advanced topic), the interpreter will inline the recursive calls - essentially converting the recursive function to a simple loop. That way you can have the best of both worlds: clean, compact code, yet efficient interpreted code and execution.
I also show the more compact way to display your output to the screen. You only need one call to put_line; let the "if" (or "case expression") take care of what is to be displayed within the put_line call. Moreover, the common part, 'Divisible', can be factored out; the case expression simply adds 'Not ' when it's needed.
declare
n number := &input_number;
function sum_of_digits(n integer) return integer is
begin
return mod(n, 10) + case when n < 10 then 0
else sum_of_digits(trunc(n/10)) end;
end;
begin
dbms_output.put_line(
case when mod(n, sum_of_digits(n)) != 0 then 'Not' end || 'Divisible');
end;
/
Notice one more thing: the outer block has a variable called n, but the function (in the declare section) also has a variable n. For the duration of the nested block (the function definition), n means the local variable. You need to always pay attention to such "masking" of an outer variable by a local variable by the same name.
I'm trying to do it as fast as it is possible. What I can not figure out is how to put all the divisors into the array, and sort that array after that.
I've optimized the for loop - it ends with sqrt(n).
I've also refactored my code, but it still doesn't pass all the tests
type output = array of longint;
var
grater,lower: output;
n,i,v,counter:longint;
begin
read(n);
setLength(grater, round(Sqrt(n)));
setLength(lower, round(Sqrt(n)));
counter:= 0;
for i:=1 to round(Sqrt(n)) do
begin
if (n mod i = 0) then
begin
if i>round(Sqrt(n)) then
grater[counter]:= i
else
lower[counter]:=i;
if n div i>round(Sqrt(n)) then
grater[counter]:= n div i
else
lower[counter]:=n div i;
counter:= counter +1;
end;
end;
for v:=0 to Length(lower) do
begin
if (lower[v] <> 0) then writeln(lower[v]);
end;
for v:=Length(grater)-1 downto 0 do
begin
if grater[v] <> 0 then writeln(grater[v]);
end;
end.
It looks like what you're doing is this:
check all integers from 2 up to sqrt(n)
if the input is divisible by the integer, record the integer and (input/integer)
So for input 12, your output might look like:
2
6
3
4
An easy way to adjust what you have is to use two lists for your answers: the first list will record factors less than sqrt(input) in ascending order, and the second will record factors greater than sqrt(input) in descending order. Then, to print them out in order, simply print the contents of the first list in order, and follow up with the contents of the second list in reverse order.
I'm having the following program:
var
N, M, Result: Real;
begin
Readln(N);
Readln(M);
if (N > 0) and (M > 0) then
Result := N / M;
Writeln(Result:10);
Readln();
end.
and I want the result to be in a normal form, not the exponential form (for example, 8.29 instead of 8.29E+000)
Try this:
Writeln (Result:10:2);
The 2 means "2 decimal places."
http://wiki.freepascal.org/Formatting_output
Use the format specifiers of Writeln, like
Writeln(Result:10:2), for 2 decimal digits.
Besides, your result variable will be undefined if N or M are <0. It's better to initialize it, e.g. to 0.
I'm a beginner and I'm trying to write a simple program that will calculate all the dividers of a number. After doing a division I want to write only the dividers that give me a natural number. I can't think of a way to do it.
Loop with dividing the number
For i := 1 to x do
Begin
D := x div i;
WriteLn ('Divider', lp, '. ', x, ' : ', i, ' = ', D);
lp := lp +1;
End;
Here's a hint - rather than checking whether the result of x div i is natural, why not check whether x is divided exactly by i? Meaning that x mod i should be 0.