Please tell me the problem in the code. I have written this code and its not working. Tell me the mistakes or if there is any other and easy method to generate prime numbers till 1000.
declare
i number;
prime number;
j number;
begin
for i in 2 .. 1000 loop
prime := 0;
for j in 2 .. i/2 loop
if mod(i,j)=0 then prime := 1
end if;
end loop;
if prime = 0 then dbms_output.put_line(i||'&');
end if;
end loop;
end;
You already have your answer (missing semicolon), but just for fun:
The i variable declared at the top is not used.
In theory j would be more efficient as a pls_integer (as i is implicitly). Possibly even a simple_integer, but then you'd need to restructure the loop to make i a simple_integer as well, and it's barely worth it for the tiny fraction of a second you might gain, if the compiler hasn't already optimised it.
You might as well exit the inner loop at the first match, rather than checking every single number.
prime would be more readable as a Boolean.
On the subject of readability, it is standard practice to align end loop under its opening loop statement.
I'm not seeing the point of appending & to every line of output.
This gives me:
declare
j pls_integer;
prime boolean;
begin
for i in 2 .. 1000 loop
prime := true;
for j in 2 .. i/2 loop
if mod(i,j) = 0 then
prime := false;
exit;
end if;
end loop;
if prime then
dbms_output.put_line(i);
end if;
end loop;
end;
You have missed one semicolon and try to put set server output on then run it
set serveroutput on
declare
i number;
prime number;
j number;
begin
for i in 2 .. 1000 loop
prime := 0;
for j in 2 .. i/2 loop
if mod(i,j)=0 then prime := 1;
end if;
end loop;
if prime = 0 then dbms_output.put_line(i||'&');
end if;
end loop;
end;
/
Related
I have made a program for obtaining a list of prime numbers in ADA and using the following online compiler:
https://rextester.com/l/ada_online_compiler
My code is the following:
--GNAT 8.3.0
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO;
procedure prime is
function isPrime(n:in Integer) return Boolean is
begin
for i in 2..n loop
if n mod i=0 then
return False;
end if;
end loop;
return True;
end isPrime;
begin
for i in 1..100 loop
if isPrime(i)=True then
Ada.Text_IO.Put_Line(Integer'Image(i));
end if;
Ada.Text_IO.Put_Line(Integer'Image(i));
end loop;
end prime;
And instead of printing a list of primes it only print 1. I have program the same code in C and no problem at all.
Your for loop in isPrime() checks every value higher than one as "n mod n = 0" which will cause you to return false for every value higher than 1. Change the for loop condition to
for i in 2..(n-1) loop
and work from there
Expanding on Jere's approach, several simple primality tests will simplify the divisibility test in the isPrime loop:
The only even prime, 2, can be handled immediately:
if N = 2 then
return True;
end if;
All remaining even numbers can be eliminated:
if N mod 2 = 0 then
return False;
end if;
This leaves odd numbers in the range 3 .. √N to check:
for i in 3 .. Positive (Sqrt (Float (N))) loop
if N mod i = 0 then
…
end if;
end loop;
It is even better by defining a Prime_Number type containing prime numbers.
subtype Prime_Number is Positive range 2 .. Positive'Last with
Dynamic_Predicate => (for all I in 2 .. (Prime_Number / 2)
=> (Prime_Number mod I) /= 0);
Then, use the code segment below to print out all prime numbers between 2 .. 100 range.
for Index in Positive range 2 .. 100 loop
if Index in Prime_Number then
Put_Line ("Prime number: " & Index'Image);
end if;
end loop;
declare
K number;
Lnumber;
f number;
function prime(n in number, I in number) return is flag
begin
i:=2;
flag:=1;
n:=&n;
for i in 2..n/2
loop
if mod(n,i)=0
then
flag:=0;
exit;
end if;
end loop;
if flag=1
then
dbms_output.put_line('prime');
else
dbms_output.put_line('not prime');
end if;
return flag;
end;
begin
k:=4;
L:=1;
f:= prime(n,i);
dbms_output.put_line(given number is ||flag);
end;
/
I am getting output as this I wonder what is wrong with my code:
Enter value for n: 4
old 9: n:=&n;
new 9: n:=4;
declare
*
ERROR at line 1:
ORA-06540: PL/SQL: compilation error
ORA-06553: PLS-906: Compilation is not possible
q2
SQL> DECLARE
num NUMBER;
3 c NUMBER;
4
5 PROCEDURE fact (x IN NUMBER, f OUT NUMBER)
6 IS
7 l_var NUMBER := 1;
8 BEGIN
9 FOR i IN 1 .. x
10 LOOP
11 l_var := l_var * i;
12 END LOOP;
13
14 f := l_var;
15 END;
16 BEGIN
17 num := 6;
18 fact (num, c);
19 DBMS_OUTPUT.put_line (' Factorial of ' || num || ' is ' || c);
20 END;
.
in this 2nd code the result is not getting displayed , not only for this even for other codes it is showing the line PL/SQL procedure successfully completed. but the results are not displaying what is wrong with it
A fixed version of q1 might be something like this (comments inline):
declare
-- k number; -- not used
-- l number; -- previously "lnumber;" not used anyway
f number;
function prime
( n in number ) -- "i" parameter never used
return number -- added return type
is
flag number := 1; -- added datatype, initial value and semicolon
begin
-- i := 2; -- Can't modify an IN parameter
-- flag := 1; -- not needed, can assign a value when declaring
-- n := &n; -- Can't modify an IN parameter
for i in 2 .. n / 2 loop
if mod(n, i) = 0 then
flag := 0;
exit;
end if;
end loop;
if flag = 1 then
dbms_output.put_line('prime');
else
dbms_output.put_line('not prime');
end if;
return flag;
end prime; -- added closing tag for clarity
begin
-- k := 4; -- not used
-- l := 1; -- not used
-- f := prime(n, i); -- n and i are not declared
f := prime(123);
dbms_output.put_line('prime check returns '|| f); -- added quotes, changed "flag" to "f"
end;
Also I formatted the code to make it more readable. This also helps when writing code, as it keeps the structure clearly visible and makes some errors more obvious.
q2 works for me, so you probably just need to check how whatever tool you are using displays dbms_output. In some tools there is a checkbox, others require a set server output on command.
You can also provide a function implementation of your factorial procedure:
declare
num number;
c number;
procedure fact
( num in number
, f out number )
is
begin
f := 1;
for i in 2 .. num loop
f := f * i;
end loop;
end fact;
function fact
( num number )
return number
is
f number;
begin
fact(num, f);
return f;
end fact;
begin
num := 6;
fact(num, c);
dbms_output.put_line('Factorial of ' || num || ' is ' || c);
dbms_output.put_line('Factorial of ' || num || ' is ' || fact(num));
end;
I've to create a function which print the Fibonacci series as its result. I've used a varray in the program below but it is giving me an error saying "PLS-00201: identifier 'ARRAY' must be declared" on line no. 2.
create function fibonacci7(x int)
return VARRAY
is
type fib IS VARRAY(25) OF VARCHAR(10);
a number(3):=1;
b number(3):=1;
c number(3);
i number(3):=1;
begin
while a<=n
loop
fib(i) := a;
c:=a+b;
a:=b;
b:=c;
i:=i+1;
end loop;
return codes_;
end ;
/
select fibonacci7(5) from dual;
I think this will do what you want. VARRAY's have a little different syntax than what you were using.
set serveroutput on size 1000000
create or replace type fibtype AS VARRAY(25) OF NUMBER;
/
create or replace function fibonacci7(n number)
return fibtype
is
fib fibtype := fibtype();
a number:=1;
b number:=1;
c number;
i number:=1;
begin
fib.extend(n);
while i<=n
loop
fib(i) := a;
c:=a+b;
a:=b;
b:=c;
i:=i+1;
end loop;
return fib;
end ;
/
declare
i number;
fib fibtype := fibtype();
begin
fib := fibonacci7(6);
for i in 1..fib.count loop
dbms_output.put_line(to_char(fib(i)));
end loop;
end;
/
Here is the output.
1
1
2
3
5
8
Bobby
p.s. Fixed to work with fib(6) and made array numbers
I am writing this code in Ada for a class where we have to teach ourselves the code. I understand heap sort, but the Ada syntax is really confusing me. I don't understand why I am getting a constraint error in this sort function.
Essentially we have to pass array "A" into this procedure, and it should organize it. I get the constraint error at siftDown(A(Start...A'Last));
Thank you in advance
Procedure sort_3(A : in out array_type) is
procedure swap(Left : in out Integer; Right : in out Integer) is
temp : Integer;
begin
temp := Left;
Left := Right;
Right := Temp;
end swap;
procedure siftDown(A : in out array_type) is
Count : Integer := 1;
root : Integer := Integer'Pos(A'First);
child : Integer := Integer'Pos(A'Last);
last : Integer := Integer'Pos(A'Last);
begin
while root * 2 + 1 <= last loop
child := root * 2 + 1;
if child + 1 <= last and then A(Integer'Val(child)) < A(Integer'Val(child + 1)) then
child := child + 1;
end if;
if A(Integer'Val(root)) < A(Integer'Val(child)) then
swap(A(Integer'Val(root)), A(Integer'Val(child)));
root := child;
else
exit;
end if;
end loop;
end siftDown;
procedure heapify(A : in out array_type) is
Count : Integer := 0;
First_Pos : Integer;
Last_Pos : Integer;
Start : Integer;
begin
First_Pos := A'First;
Last_Pos := A'Last;
Start := Integer'Val((Last_Pos - First_Pos + 1) / 2);
loop
siftDown(A(Start...A'Last));
if Start > Integer'First then
Start := Integer'Pred(Start);
else
exit;
end if;
end loop;
end heapify;
Last_Index : Integer := Integer'Last;
begin
heapify(A);
while Last_Index > Integer'First loop
swap(A(Last_Index), A(A'First));
Last_Index := Integer'Pred(Last_Index);
siftDown(A(A'First..Last_Index));
end loop;
end sort_3;
You have a syntax error in the code - an extra dot in A(Start...A'Last).
The syntax A(Start..A'Last) means a slice, part of array from Start to the last element. The Constraint_Error means that Start not in array bounds. Try to add
Ada.Text_IO.Put_Line (Start'Image);
before that line and you will see Start values and when it became out of the A'Range.
Your code has some references to Integer'First and Integer'Last, which are huge values that have nothing to do with the array A and its values. I'm pretty sure you should use A'First and A'Last instead.
Also a note on style: Using the same identifier, "A", for the parameter of the local (inner, nested) procedures as for the parameter "A" of the containing (outer) procedure, when these arrays can be different, invites confusion and errors. Better to use different identifiers.
I need to write recursive function to count amount of odd numbers in the sequence
Here my initial code:
program OddNumbers;
{$APPTYPE CONSOLE}
uses
SysUtils;
function GetOddNumbersAmount(const x: array of integer; count,i:integer):integer;
begin
if((x[i] <> 0) and (x[i] mod 2=0)) then
begin
count:= count + 1;
GetOddNumbersAmount:=count;
end;
i:=i+1;
GetOddNumbersAmount:=GetOddNumbersAmount(x, count, i);
end;
var X: array[1..10] of integer;
i,amount: integer;
begin
writeln('Enter your sequence:');
for i:=1 to 10 do
read(X[i]);
amount:= GetOddNumbersAmount(X, 0, 1);
writeln('Amount of odd numbers: ', amount);
readln;
readln;
end.
When i type the sequence and press "enter", program closed without any errors and i can't see the result.
Also, i think my function isn't correct.
Can someone help with that code?
UPD:
function GetOddNumbersAmount(const x: array of integer; count,i:integer):integer;
begin
if((x[i] <> 0) and (x[i] mod 2<>0)) then
count:= count + 1;
if(i = 10) then
GetOddNumbersAmount:=count
else
GetOddNumbersAmount:=GetOddNumbersAmount(x, count, i+1);
end;
You don't provide an end of recursion, i.e., you always call your function GetOddNumbersAmount again, and your program never terminates. Thus, you get an array index error (or a stack overflow) and your program crashes.
Please note, that every recursion need a case where it terminates, i.e. does not call itself. In your case, it should return if there are no elements in the array left.
In addition, you are counting the even numbers, not the odd ones.
You passed a static array to a dynamic so the index get confused:
Allocat the array with
SetLength(X,10)
allocates an array of 10 integers, indexed 0 to 9.
Dynamic arrays are always integer-indexed, always starting from 0!
SetLength(X,10)
for it:=0 to 9 do begin
X[it]:= random(100);
And second if you know the length a loop has more advantages:
function GetEvenNumbersAmount(const x: array of integer; count,i:integer):integer;
begin
for i:= 0 to length(X)-1 do
if((x[i] <> 0) and (x[i] mod 2=0)) then begin
inc(count);
//write(inttostr(X[i-1])+ ' ') :debug
end;
result:=count;
end;