Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Another request for help
11-11-2009, 06:46 AM
Post: #11
RE: Another request for help
Okay i just changed a few lines so you just have to pass to the function number in array and it will calculate x(that_number).
Here is the whole code, i tested it wand it works.
Code:
program array_test;
uses wincrt;
var
  n:longint;
function prime(k:longint):boolean;
var
  i,g:longint;
  b:boolean;
begin
  if k mod 2 = 0 then
  if k=2 then
  prime:=true
  else
  prime:=false
  else
  begin
  b:=false;
  g:=round(sqrt(k));
  i:=4;
  repeat
    begin
      if k mod i = 0 then
      b:=true;
      i:=i+2;
    end;
  until (i>g) or (b=true);
  if b=true then
    prime:=false
  else
    prime:=true;
  end;
end;
function generate(k:longint):longint;
var
i,j,g,d,br,f:integer;
a:array[1..100] of integer;
begin
  br:=0;
    if k<5 then
      f:=k+3
    else
     f:=k+1;
   for i:=1 to f do
     if not prime(i) then
      begin
       br:=br+1;
       a[br]:=i;
       for j:=i-1 downto 2 do
        if i mod j = 0 then
         begin
           d:=i div j;
           for g:=1 to d do
            begin
             br:=br+1;
             a[br]:=d;
            end;
         end;
       end;
     generate:=a[k];
end;
begin
readln(n);
writeln(generate(n));
end.
BTW if you have code for filling the matrix, why don't you post it, i would like to see your approach to this problem. It is on first look easy, but you need to draw it to understand it etc.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
11-11-2009, 07:34 AM (This post was last modified: 11-11-2009 06:04 PM by Dwel.)
Post: #12
RE: Another request for help
I only have it on paper. I need to test it first. I will post it as soon as I get the whole program done.
Hmm, your generation code kinda breaks when it reaches ~23 values. It goes - . . . . 8 2 2 4 4 4 4 10 2 2 5 1 1 1 1 1 1 1 (the ones here kinda go on until they start with 5's again and then it gets weirder).
Actually it skips 9 here and goes to 10 where it breaks at the 5's.
This is the part of the code that fills the n columns and n rows matrix in a spiral manner by calling the next value at each step. Sadly, I still haven't succeded in getting the generation implemented.

Code:
var i,j,c:integer;
Begin

i:=1;
j:=1;
c:=2;

// m and n are the number of columns and rows of the matrix -- but in the problem statement they are both equal to n since the matrix is [1..n,1..n]

Mat[i,j]:=@GetNextNumber // -> Mat[1,1] gets the first value, which should be 4.
                         // @GetNextNumber - the part I don't know how to get running, should call the next number in the sequence, in this case the first.

While n*n > 0 do begin     // -> not sure about this condition, needs to be tested
       while j <= n do begin
             j:=j+1;
             Mat[i,j]:=@GetNextNumber
       end;
       while i<=n do begin
             i:=1+1;
             Mat[i,j]:=@GetNextNumber
       end;
       while not(j=c-1) do begin
             j:=j-1;
             Mat[i,j]:=@GetNextNumber
       end;
       while not(i=c) do begin
             i:=i-1;
             Mat[i,j]:=@GetNextNumber
       end;
n:=n-1;
c:=c+1;
end;


This is just the processing. I see I haven't inlcuded the actual printing.
Find all posts by this user
Quote this message in a reply
11-11-2009, 06:32 PM (This post was last modified: 11-11-2009 06:33 PM by Dwel.)
Post: #13
RE: Another request for help
This is the whole problem as I have it. Fill a n*n matrix in a spiral manner using the values of that X (4 2 2 6 2 2 3 3 3...).

Of course, it doesn't really work for anything else than the value 1 (where it just displays 4). I used your last generation code (which is still broken after it passes the 8 sequence - 8 2 2 4 4 4 4 -> 10 / It skips the 9 and then becomes weird). And I've added the spiral filling I thought should work...but it doesn't for some reason.

Code:
Program PopulateMatrix;
uses crt;

var    temp,size,i,j,c,z,m:integer;
        Mat:array[1..100,1..100] of integer;



function prime(k:integer):boolean;
var
  i,g:integer;
  b:boolean;
begin
  if k mod 2 = 0 then
  if k=2 then
  prime:=true
  else
  prime:=false
  else
  begin
  b:=false;
  g:=round(sqrt(k));
  i:=4;
  repeat
    begin
      if k mod i = 0 then
      b:=true;
      i:=i+2;
    end;
  until (i>g) or (b=true);
  if b=true then
    prime:=false
  else
    prime:=true;
  end;
end;

function generate(k:integer):integer;
var
i,j,g,d,br,f:integer;
a:array[1..100] of integer;
begin
  br:=0;
    if k<5 then
      f:=k+3
    else
     f:=k+1;
   for i:=1 to f do
     if not prime(i) then
      begin
       br:=br+1;
       a[br]:=i;
       for j:=i-1 downto 2 do
        if i mod j = 0 then
         begin
           d:=i div j;
           for g:=1 to d do
            begin
             br:=br+1;
             a[br]:=d;
            end;
         end;
       end;
     generate:=a[k];
end;



Begin

readln(m);

temp:=m;
i:=1;
j:=1;
c:=2;
z:=1;
size:=m*m;

Mat[i,j]:=generate(z);
z:=z+1;

While size > 0 do begin
       while (j<=m) do begin
             j:=j+1;
             Mat[i,j]:=generate(z);
             z:=z+1;
       end;
       while (i<=m) do begin
             i:=1+1;
             Mat[i,j]:=generate(z);
             z:=z+1;
       end;
       while not(j=c-1) do begin
             j:=j-1;
             Mat[i,j]:=generate(z);
             z:=z+1;
       end;
       while not(i=c) do begin
             i:=i-1;
             Mat[i,j]:=generate(z);
             z:=z+1;
       end;

m:=m-1;
c:=c+1;
size:=size-1;

end;

m:=temp;

for i:=1 to m do
    begin
      for j:=1 to m do
        write(' ',Mat[i,j],'   ');
      writeln;
      writeln;
    end;



readkey;
end.
Find all posts by this user
Quote this message in a reply
11-12-2009, 03:33 AM
Post: #14
RE: Another request for help
I think i know where is the mistake . . .
But the problem is that your matrix spiral filling doesn't work for me it enters in dead loop.
There tasks are not heavy, the problem is time . . .
I have to think about that again !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
11-12-2009, 03:45 AM
Post: #15
RE: Another request for help
Okay i have found a mistake in generation, try now.
There is a possibility to have that sequence of ones, but very far, i can make it better, but it is good like this. Test it on 40, 50, 100 it will be okay.
Code:
program array_test;
uses wincrt;
var
  n:longint;
function prime(k:longint):boolean;
var
  i,g:longint;
  b:boolean;
begin
  if k mod 2 = 0 then
  if k=2 then
  prime:=true
  else
  prime:=false
  else
  begin
  b:=false;
  g:=round(sqrt(k));
  i:=4;
  repeat
    begin
      if k mod i = 0 then
      b:=true;
      i:=i+2;
    end;
  until (i>g) or (b=true);
  if b=true then
    prime:=false
  else
    prime:=true;
  end;
end;
function generate(k:longint):longint;
var
i,j,g,d,br,f:integer;
a:array[1..1000] of integer;
begin
  br:=0;
    if k<5 then
      f:=k+3
    else
     f:=k+1;
   for i:=1 to f do
     if not prime(i) then
      begin
       br:=br+1;
       a[br]:=i;
       for j:=i-1 downto 2 do
        if i mod j = 0 then
         begin
           d:=i div j;
           for g:=1 to d do
            begin
             br:=br+1;
             a[br]:=d;
            end;
         end;
       end;
     generate:=a[k];
end;
begin
readln(n);
writeln(generate(n));
end.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
11-12-2009, 03:50 PM
Post: #16
RE: Another request for help
The new version that should fix the spiral filling. But it still has a few bugs. With the new generation added.

Code:
Program PopulateMatrix;
uses crt;

var    temp,size,i,j,c,z,m:integer;
        Mat:array[1..100,1..100] of integer;



function prime(k:integer):boolean;
var
  i,g:integer;
  b:boolean;
begin
  if k mod 2 = 0 then
  if k=2 then
  prime:=true
  else
  prime:=false
  else
  begin
  b:=false;
  g:=round(sqrt(k));
  i:=4;
  repeat
    begin
      if k mod i = 0 then
      b:=true;
      i:=i+2;
    end;
  until (i>g) or (b=true);
  if b=true then
    prime:=false
  else
    prime:=true;
  end;
end;

function generate(k:integer):integer;
var
i,j,g,d,br,f:integer;
a:array[1..1000] of integer;
begin
  br:=0;
    if k<5 then
      f:=k+3
    else
     f:=k+1;
   for i:=1 to f do
     if not prime(i) then
      begin
       br:=br+1;
       a[br]:=i;
       for j:=i-1 downto 2 do
        if i mod j = 0 then
         begin
           d:=i div j;
           for g:=1 to d do
            begin
             br:=br+1;
             a[br]:=d;
            end;
         end;
       end;
     generate:=a[k];
end;



Begin

readln(m);

temp:=m;
i:=1;
j:=1;
c:=2;
z:=1;
size:=m div 2;

Mat[i,j]:=generate(z);
z:=z+1;

While size > 0 do begin

       repeat
             If j=m then begin j:=j
                         end
                    else begin
                        j:=j+1;
                        Mat[i,j]:=generate(z);
                        z:=z+1;
                         end
       until j=m;
       repeat
             If i=m then begin i:=i
                         end
                    else begin
                         i:=i+1;
                         Mat[i,j]:=generate(z);
                         z:=z+1;
                         end
       until i=m;
       repeat
             If j=c-1 then begin j:=j;
                           end
                      else begin
                            j:=j-1;
                            Mat[i,j]:=generate(z);
                            z:=z+1;
                           end
       until j=c-1;
       repeat
             If i=c then begin i:=i;
                         end
                    else begin
                         i:=i-1;
                         Mat[i,j]:=generate(z);
                         z:=z+1;
                        end
       until i=c;

m:=m-1;
c:=c+1;
size:=size-1;

end;
z:=z+1;
m:=temp;

If m mod 2 = 0 then begin
                     end
                else begin  i:=m+1 div 2;
                            j:=m+1 div 2;
                            Mat[i,j]:=generate(z);
                     end;



for i:=1 to m do
    begin
      for j:=1 to m do
        write(' ',Mat[i,j],'   ');
      writeln;
      writeln;
    end;



readkey;
end.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: