用Matlab 写一个m文件,从1到100中随机抽取50个数字,输出其间的素数,并进行降序排列,最后求和。求程序

不是C++啊,谢谢

参考代码:

N=randi(100,1,50)
M=N(isprime(N))
M=sort(M,'descend')
sum(M)

 

某次运行结果(有随机数,每次运行结果不同):

>> N=randi(100,1,50)
N =
  Columns 1 through 9
    80    81     6    41    97    42    34    64    86
  Columns 10 through 18
    79    54    75    74    45    86    50    95    80
  Columns 19 through 27
    14    24    13    54     6    47    99    69    88
  Columns 28 through 36
    50    29    19    97    76    96    68    19    29
  Columns 37 through 45
     6     5    28    18    68    91    36    11    80
  Columns 46 through 50
    47    69    69    43    66
>> M=N(isprime(N))
M =
  Columns 1 through 9
    41    97    79    13    47    29    19    97    19
  Columns 10 through 14
    29     5    11    47    43
>> M=sort(M,'descend')
M =
  Columns 1 through 9
    97    97    79    47    47    43    41    29    29
  Columns 10 through 14
    19    19    13    11     5
>> sum(M)
ans =
   576

温馨提示:答案为网友推荐,仅供参考
相似回答