Message Boards Message Boards

0
|
5079 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

HELP with Mathematica Homework

Posted 9 years ago

This is PROBLEM I HAVE TO DO.

Write a module that takes as input a positive integer m prints the first m positive odd numbers and returns the sum of the numbers it printed. You should use the Print command in the module. Look it up in the Wolfram Documentation center available under the Help tab. Run this module for 3 different numbers between 20 and 50.

POSTED BY: Erly Solis
2 Replies

Hi,

sounds like homework. This should work:

f[m_Integer] := Module[{k = m}, Table[Print[2 (j - 1) + 1], {j, 1, k}]; 
  StringJoin[{{"The sum is ", ToString[Evaluate[Total[Table[2 (j - 1) + 1, {j, 1, k}]]]]}, "."}]]

You can easily evaluate that for three different numbers.

The print command is somewhat artificial, because table does it anyway. But here is a variant where the Print is important:

g[m_Integer] := Module[{k = m}, For[j = 1, j <= k, j++, Print[2 (j - 1) + 1]]; 
  StringJoin[{{"The sum is ", ToString[Evaluate[Total[Table[2 (j - 1) + 1, {j, 1, k}]]]]}, "."}]]

Cheers,

M.

POSTED BY: Marco Thiel

Wolfram Community is only for the discussions of Wolfram Technologies.

Other unrelated topics (for example, obvious homework requests, or purely scientific topics) should be posted on different, appropriate forums.

We are very grateful to all contributors offering help and contributing to Community.

But unfortunately we cannot support discussions outside Community guidelines:

http://community.wolfram.com/guidelines

If you see inappropriate discussions please do not respond in future but let moderators know by flagging the post or emailing at:

wolfram-community@wolfram.com

Thank you for your patience and understanding!

Sincerely, Moderation Team

POSTED BY: Moderation Team

Group Abstract Group Abstract