Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Another request for help
11-10-2009, 11:01 AM
Post: #1
Another request for help
Hello,

I need a bit of help with something again Angel.


So, I have X = [4, 2, 2, 6, 2, 2, 3, 3, 3, 8, 2, 2, 4, 4, 4, 4, 9, 3, 3, 3,...]

Basically, it contains only the numbers which have divisors (excluding numbers that are only divisible with themselves) and their divisors written as many times as they are worth and the divisors are taken from the smallest to the biggest.

Now, I have to write a function ( or a set of functions) that can generate the values in X as they are called. (without memorizing the vector X).
So if I would call .. Generate(...); 4 times, I would get 4, then 2, then 2, and then 6. and so on as they are called.
Again, X can't be memorized.
I've been trying to get this done for a few hours now and I can't make head or tails of something that works.
Any help would be appreciated.


Thanks.
Find all posts by this user
Quote this message in a reply
11-10-2009, 11:06 AM
Post: #2
RE: Another request for help
(11-10-2009 11:01 AM)Dwel Wrote:  Hello,

I need a bit of help with something again Angel.


So, I have X = [4, 2, 2, 6, 2, 2, 3, 3, 3, 8, 2, 2, 4, 4, 4, 4, 9, 3, 3, 3,...]

Basically, it contains only the numbers which have divisors (excluding numbers that are only divisible with themselves) and their divisors written as many times as they are worth and the divisors are taken from the smallest to the biggest.

Now, I have to write a function ( or a set of functions) that can generate the values in X as they are called. (without memorizing the vector X).
So if I would call .. Generate(...); 4 times, I would get 4, then 2, then 2, and then 6. and so on as they are called.
Again, X can't be memorized.
I've been trying to get this done for a few hours now and I can't make head or tails of something that works.
Any help would be appreciated.


Thanks.

can you just pass the reference of x to another variable?
Visit this user's website Find all posts by this user
Quote this message in a reply
11-10-2009, 11:11 AM (This post was last modified: 11-10-2009 11:16 AM by Dwel.)
Post: #3
RE: Another request for help
Well, X is just as a guideline of what values I will have to use later in the code. But I can't get them by manually inserting them. I have to call them (a.k.a. generating them as I need them).

For example :

With

Code:
function NextDiv(nr,d:integer):integer;
var i:integer;
    t:boolean;
begin
t:=true;
i:= d +1;
while((i<=(nr div 2)) and t) do
  begin
    if(nr mod i = 0) then
        t:=false;
    i:=i+1;
  end;

if t then
    NextDiv:= d
else
    NextDiv:=i-1;

end;

function Gen(var nr,d:integer):integer;
var temp:integer;

begin
    temp:=NextDiv(nr,d);
    if(temp = d) then
        begin
            nr:=nr+1;
            d:=1;
            temp:=nr;
        end
    else
        d:=temp;

Gen:=temp;

end;


If I would keep calling Gen(...) I'd get the values of Y = [1, 2, 3, 4, 2, 5, 6, 2, 3, 7, 8, 2, 4, 9, 3, 10, 2, 5, 11, 12, ...]. Which is different from my X now, but illustrates what I'm looking at doing for it.
I tried to alter this to make it adapt to X, but to no succes.
Find all posts by this user
Quote this message in a reply
11-10-2009, 11:28 AM
Post: #4
RE: Another request for help
I would say pass by ref
Visit this user's website Find all posts by this user
Quote this message in a reply
11-10-2009, 08:42 PM
Post: #5
RE: Another request for help
I have solution for this. It is a bit complicated and it can be optimized, but i will post it like this because it works.
The fact is that you need to check numbers that have divisors, or better said numbers that are not prime, so i made function for checking if numbers are prime. After that you need to divide that number on it's factors and to write them that number of times as they are worth.
I made that like you said generate() function that generates that.
So here is the solution :
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;
procedure generate(k: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;
       for i:=1 to k do
         write(a[i],' ');
end;
begin
readln(n);
generate(n);
end.
As you can see in the main program you have only reading of "n" and calling generate() on "n".
Test it, i tested it and it works perfectly !

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, 02:39 AM
Post: #6
RE: Another request for help
Sorry for the delay, but I wasn't home.

I have read the program this morning and tried it and as far as the values generated go, it works perfectly.
But in the main program, I will have to complete yet another matrix by calling the values of X (4..2..2..6..etc), without memorizing it. As the matrix will get populated in a weird sequence, I will have to call the generation every step of the way ( in the matrix, it starts at [i,j] = [1,1], where the generate would give it 4, then the j would increase to 2, [1,2] so the generation call should return 2, and so on).
Find all posts by this user
Quote this message in a reply
11-11-2009, 04:15 AM
Post: #7
RE: Another request for help
So you mean you have sequence 4,2,2,6,2,2,3,3,3. . .
And that should be in matrix, but i don't know which length of matrix do you want. I mean that "m" and "n" !
Tell me example, what should matrix look like.
Does it have to look like you enter m and n and it will generate numbers like this :
m=3 and n=3
4 2 2
6 2 2
3 3 3

I don't understand the task !

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, 04:34 AM (This post was last modified: 11-11-2009 04:54 AM by Dwel.)
Post: #8
RE: Another request for help
So. the values, that sequence 4.2.2.6.2.2.3.3.3, etc, has to be generated as I call it. Basically. In the program that only does the generation code.

If i would write Generate(..);Generate(..);Generate(..);Generate(..); in the main program loop. The output would be 4 2 2 6.

Now, the matrix has to be populated in a weird manner. For example:

Code:
m and n I think should be chosen by the user.
for m=3 and n=3 then the Matrix would be

    4 2 2     The manner in which it's getting populated is  A - B - C
    3 3 6                                                    H   I   D
    3 2 2                                                    G   F   E


    So for m=4 and n=4, the Matrix would be:

     4 2 2 6
     2 4 4 2
     2 4 4 2
     8 3 3 3   and so on as the matrix is bigger

[Image: test.jpg]

The algorithm for the matrix population I have done. All i need to add to it is the CALL which would bring me the next value in the sequence. (generated on spot since the sequence isn't memorized).
To be even more clear. I need to access the values of the sequence, NOT by having an "n" that dictates how many values are generated, but to call the generation function n times. This way I can fill the matrix by calling the next value every time my Mat[i.j] moves to the new location.
Find all posts by this user
Quote this message in a reply
11-11-2009, 05:39 AM
Post: #9
RE: Another request for help
That is spiral filling of matrix. I would need time for this, this is complex issue !

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, 05:49 AM (This post was last modified: 11-11-2009 06:20 AM by Dwel.)
Post: #10
RE: Another request for help
I don't need the whole problem. Just the generation part. To me more concise, how do you alter your code, that could generate the values, in order for me to call them as I stated?

I already have the code which fills the matrix in a spiral, I just need the calling function so I can place every value in it's place as the matrix is filled.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: