Harmonic

<p>Hi, I need help.</p>
<div style="margin: 0in 0in 0pt"><span style="color: black">I need to write a program that, for input integer n &gt; 0, output the row vector [k<sub>1</sub>,k<sub>2</sub> . . . , k<sub>n</sub>].</span></div>
<div style="margin: 0in 0in 0pt"><span style="color: black">For example, when input n = 8, the output is [1, 4, 11, 31, 83, 227, 616, 1674].</span></div>
<div style="margin: 0in 0in 0pt">&nbsp;</div>
<div style="margin: 0in 0in 0pt">&nbsp;This is what I have:</div>
<pre>
harmonic1 := proc ( n )<br />&nbsp;&nbsp; local s, h, k, j;

&nbsp;&nbsp; h := LinearAlgebra[Dimension](x);<br />&nbsp;&nbsp; s := 0;

<br />&nbsp;&nbsp; for k from 1 to h do&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while n &gt; 0 do<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s := s + 1/j[K];&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end do;
&nbsp;&nbsp; end do;

<br />&nbsp;&nbsp; return s;
end proc;</pre>
<pre>
&nbsp;</pre>
<p>But is not woking.</p>
<p>Can some one help me please</p>
<p>Thanks ^_^</p>

Hi, I need help. I need to

Hi, I need help.

I need to write a program that, for input integer n > 0, output the row vector [k1,k2 . . . , kn].
For example, when input n = 8, the output is [1, 4, 11, 31, 83, 227, 616, 1674].
 
 This is what I have:

harmonic1 := proc ( n )
   local s, h, k, j;    h := LinearAlgebra[Dimension](x);
   s := 0;
   for k from 1 to h do     
      while n > 0 do

         s := s + 1/j[K];    
      end do;    end do;
   return s; end proc;
 

But is not woking.

Can some one help me please

Thanks ^_^

one approach

The real challenge is making this reasonably fast.  Here is my first attempt, not particularly fast.  This isn't the nicest approach, from a pedagogical viewpoint.

Harmonic := module()
export ModuleApply;
local nmax;
    nmax := 1;
    ModuleApply := proc(n::posint)
    option remember;
    local k;
        procname(nmax) + add(1/k,k=nmax+1..n);
    end proc;
    ModuleApply(1) := 1;
end module:

A := proc(n::posint)
local k;
    for k while Harmonic(k) < n do end do;
    return k;
end proc:

harmonic1 := proc(n)
    Vector['row'](n, A);
end proc:

harmonic1(3);
                  [1, 4, 11]
 

 

Robert Israel's picture

What are you trying to do?

The first thing you have to do is tell us what you're trying to do.  What are k1, k2, ..., kn supposed to be?  I can't make any sense of [1, 4, 11, 31, 83, 227, 616, 1674], and neither can the Encyclopedia of Integer Sequences or Maple's guessgf
 

 

A004080

Robert Israel's picture

A004080

Yes, I saw that after I saw your posting...  My fault, I miscopied the search string.  In any case, I think my advice was sound: whether trying to do a problem oneself or asking somebody else for help, it's important to state the problem clearly.

 

 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}