如何把一个由若干质数的和组成的数分解成干质数。

waynezlee 2000-07-10 10:21:00
如:2+3+5+7+11=28
用什么算法可以把28分解成2,3,5,7,11
...全文
145 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluewater 2000-07-10
  • 打赏
  • 举报
回复
<%
const MAX=100
dim total
dim array(MAX) '存放所有小于给定数字的素数
dim a[MAX] '将N分解后得到的数字,它们的和等于N
dim r[MAX] '每一次分解后的余数
dim n,i
total=0 'total记录小于n的素数的个数
'这里假设需要分解的数字为n,你可以根据需要加一段代码即可实现用户输入
array(total)=1
'下面找出所有小于n的素数
for i=2 to n step 1
judgeit(i)
next
total=total+1
addit(n) '输出组成给定数的素数

sub addit(cal) 'cal=n
dim k,i
dim isitin
dim m,n,same
same=0
isitin=0
a(0)=r(0)=cal
k=1
do while(1)
if r(k-1)=0 then
for i=1 to k-1
if inarray(a(i)==0 then
isitin=0
endif
next
for m=1 to k-1
same=a[m]
for n=m+1 to k-1
if same=a[n] then
isitin=0
endif
next
next
if isitin=1 then
for i=1 to k-1
response.write a(i)
next
response.write "<br>"
endif
isitin=1
same=0
do while k>1 and a(k-1)=1
k--
wend
if k=1 then
exit do
endif
a(k-1)=a(k-1)-1
r(k-1)=r(k-1)+1
else
if r(k-1)<a(k-1) then
a(k)=r(k-1)
else
a(k)=a(k-1)
endif
r(k)=r(k-1)-a(k)
k=k+1
endif
wend
end sub

function inarray(temp)
dim i
for i=0 to total-1
if temp=arrar[i] then
inarray=1
exit function
endif
inarray=function
end function

sub judgeit(temp)
dim mn
dim judge=1
for mn=2 to temp-1
if (temp MOD mn)=0 then
judge=0
next
next
if judge=1 then
total=total+1
array(total)=temp
endif
end sub

%>
以上是本人由C语言程序改写成的asp程序,可能存在错误,请继续向下看!
同时附上C语言程序,经本人调试,完全正确,满足你的要求~~~~
如果有什么问题请对照下面的C程序~~

#include <stdio.h>
#define MAX 100
int total;
int array[MAX]; /*存放所有小于给定数字的素数*/
int a[MAX]; /*将N分解后得到的数字,它们的和等于N */
int r[MAX]; /*每一次分解后的余数*/
main()
{
void judgeit(int temp);
void addit(int cal);
int n,i;
total=0;
printf("\nPlease input a digist:");
scanf("%d",&n);
array[total]=1;
for(i=2;i<=n;i++)
judgeit(i);
total++;
addit(n);
getch();
}

void addit(int cal)
{
int k,i;
int isitin;
int m,n,same;
int inarray(int temp);
same=0;
isitin=1;
a[0]=r[0]=cal;
k=1;
while(1)
{
if(r[k-1]==0)
{
for(i=1;i<k;i++)
if(inarray(a[i])==0)
isitin=0;
for(m=1;m<k;m++)
{
same=a[m];
for(n=m+1;n<k;n++)
if(same==a[n])
isitin=0;
}
if(isitin==1)
{
for(i=1;i<k;i++)
printf(" %d ",a[i]);
printf("\n");
}
isitin=1;
same=0;
while(k>1&&a[k-1]==1) k--;
if(k==1) break;
a[k-1]--;
r[k-1]++;
}
else
{
a[k]=r[k-1]<a[k-1]?r[k-1]:a[k-1];
r[k]=r[k-1]-a[k];
k++;
}
}
}

int inarray(int temp)
{
int i;
for(i=0;i<total;i++)
if(temp==array[i])
return(1);
return(0);
}

void judgeit(int temp)
{
int mn;
int judge=1;
for(mn=2;mn<temp;mn++)
if(temp%mn==0)
judge=0;
if(judge==1)
{
total++;
array[total]=temp;
}
}

茂奇软件 2000-07-10
  • 打赏
  • 举报
回复
是问题的规则不清楚。
茂奇软件 2000-07-10
  • 打赏
  • 举报
回复
good question.
背包问题是可以多解的。应该找到一个最优解。
但是,我想这个问题的最优解规则不容易说清楚。
jansen zhu.
Tyro 2000-07-10
  • 打赏
  • 举报
回复
为什么不是28=2+3+23=5+23=11+17=2+7+19=……
waynezlee 2000-07-10
  • 打赏
  • 举报
回复
附加:必須是不同质数。
Tyro 2000-07-10
  • 打赏
  • 举报
回复
为什么不是28=7+7+7+7 ?

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧