ab.com コミュニティ https://www.activebasic.com/forum/ |
|
素因数分解 https://www.activebasic.com/forum/viewtopic.php?t=2498 |
ページ 1 / 1 |
作成者: | 三毛CAT [ 2008年11月15日(土) 19:55 ] |
記事の件名: | 素因数分解 |
与えられた数を素因数分解し、文字列の形で返します。 関数本体 [ここをクリックすると内容が表示されます]
コード: Function soinsuu(num As DWord) As String Dim n As DWord Dim i As DWord Dim count As DWord Dim result As String n=num i=2 result="" While n>1 count=0 While n mod i=0 count++ n=n/i Wend If count>0 Then If count=1 Then result=result+Str$(i)+"*" Else result=result+Str$(i)+"^"+Str$(count)+"*" End If i++ Wend If result="" Then result=Str$(n)+"*" End If soinsuu=Left$(result,Len(result)-1) End Function サンプルスクリプト [ここをクリックすると内容が表示されます]
コード: #N88BASIC Dim num As DWord *start Print "素因数分解する数を入力してください。" Input num Print soinsuu(num) Goto *start Function soinsuu(num As DWord) As String Dim n As DWord Dim i As DWord Dim count As DWord Dim result As String n=num i=2 result="" While n>1 count=0 While n mod i=0 count++ n=n/i Wend If count>0 Then If count=1 Then result=result+Str$(i)+"*" Else result=result+Str$(i)+"^"+Str$(count)+"*" End If i++ Wend If result="" Then result=Str$(n)+"*" End If soinsuu=Left$(result,Len(result)-1) End Function |
作成者: | 渡辺真 [ 2008年12月29日(月) 23:55 ] |
記事の件名: | 答えが、どれも 2^31になってしまいますが? |
下の問題をやってみました。 http://makotowatana.ld.infoseek.co.jp/p ... tors.shtml 答えが合わない場合があります。 使い方が悪いのですかね。 |
作成者: | 三毛CAT [ 2009年1月17日(土) 15:33 ] |
記事の件名: | DWord型なので |
Dword型を使っているので、オーバーフローの可能性があります。 スクリプト中のDWordをQWordにしてみてください。 |
ページ 1 / 1 | 全ての表示時間は UTC+09:00 です |
Powered by phpBB® Forum Software © phpBB Limited https://www.phpbb.com/ |