1
$\begingroup$

I am working on this problem and I feel like I've come so close.

Write a module that takes as input a positive integer m, prints the first m positive odd numbers and returns the square root of the sum of the numbers it printed.

This is what I have so far:

kellyrocks[m_]:=Module[{index,sum},index=0;sum=0;

If[Or[m<= 0,IntegerQ[m]==False],Return["Please enter a positive integer"]];

While[index<=m,sum=sum+index;index=2*index+1];

Return[sqrt[sum]]]

But when I try to run it I'm not getting what I should, ex:

kellyrocks[22]

sqrt[26]

I'm very new to mathematica and I'm not sure what I'm missing. I also am not sure about printing each number. Thank you in advance!

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Dr. belisarius Mar 11 '16 at 3:45
4
$\begingroup$
g[n_Integer?(Positive@# &)] := (Print@#; Sqrt@Tr@#) &@Range[1, 2 n - 1, 2]

g[3]

(*
  {1,3,5}
  3
*)
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ The square root of 9 (1+3+5) is certainly not 35 $\endgroup$ – RunnyKine Mar 11 '16 at 2:47
  • 1
    $\begingroup$ But, but, the square root of 9 is 3. It should be Sqrt@Tr@# $\endgroup$ – RunnyKine Mar 11 '16 at 2:50
  • 2
    $\begingroup$ @RunnyKine Damn. You're too clever. Clearly an outlier :) $\endgroup$ – Dr. belisarius Mar 11 '16 at 2:51
  • 1
    $\begingroup$ The answer is always going to be an integer, in fact, its always going to be n, so the N is probably not required. $\endgroup$ – RunnyKine Mar 11 '16 at 3:09
  • 1
    $\begingroup$ @Kelly Oh, well. I disagree. You came here seeking for help and you should pay with the same coin you got. $\endgroup$ – Dr. belisarius Mar 11 '16 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.