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$
1
  • $\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$ Commented Mar 11, 2016 at 3:45

1 Answer 1

4
$\begingroup$
g[n_Integer?(Positive@# &)] := (Print@#; Sqrt@Tr@#) &@Range[1, 2 n - 1, 2]

g[3]

(*
  {1,3,5}
  3
*)
$\endgroup$
13
  • 1
    $\begingroup$ The square root of 9 (1+3+5) is certainly not 35 $\endgroup$
    – RunnyKine
    Commented Mar 11, 2016 at 2:47
  • 1
    $\begingroup$ But, but, the square root of 9 is 3. It should be Sqrt@Tr@# $\endgroup$
    – RunnyKine
    Commented Mar 11, 2016 at 2:50
  • 2
    $\begingroup$ @RunnyKine Damn. You're too clever. Clearly an outlier :) $\endgroup$ Commented Mar 11, 2016 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
    Commented Mar 11, 2016 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$ Commented Mar 11, 2016 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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