timur77
Junior Member
Someday I will tell my grandsons that I am older than the Internet. And it will blow their brain.
Posts: 79
|
Post by timur77 on Apr 1, 2020 4:04:41 GMT -5
Program - decompositions into prime factors and finding a prime number
[m0]
input "Введите положительное, целое число, больше 1:";a1
if a1<2 then goto [m0]
a=a1
[m1]
b=int(a/2)
c=a-b*2
if c=0 Then print b;"|2":a=b:goto [m1]
[m2]
b=int(a/3)
c=a-b*3
if c=0 Then print b;"|3":a=b:goto [m2]
[m3]
b=int(a/5)
c=a-b*5
if c=0 Then print b;"|5":a=b:goto [m3]
i=7
aa=sqr(a)
[m5]
if i>aa then goto [m6]
for e=0 to 6 step 2
[m4]
i1=i+e
if (a mod i1)=0 Then print a/i1;"|";i1: a=a/i1: aa=sqr(a): goto [m4]
next e
i=i+10
goto [m5]
[m6]
if a=a1 or a1=2 or a1=3 or a1=5 then print a1;"-простое"
goto [m0]
|
|
|
Post by svajoklis on Apr 27, 2020 15:26:32 GMT -5
О, вы из Росии? Извините, мой Руский язик не очен хорошый)) To translate: oh, you are from Russia? Sorry, my russian is not very good Anywho, I tried to work on your program a bit. A few more lines, but imho much more readable. Interesting algorithm! while 1 input "Enter a whole number bigger than 1 (-1 to exit): "; number
if number = -1 then exit while
if int(number) = number and number > 1 then call doNumber number else print "Bad number, try again!" end if wend
end
' divide number until a prime is reached ' print intermediate divisions sub doNumber number initialNumber = number knownPrime = 0
if (initialNumber = 2) or _ (initialNumber = 3) or _ (initialNumber = 5) then knownPrime = 1
' haven't figured out if prime or not yet if knownPrime = 0 then
call divideWhilePossibleBy number, 2 call divideWhilePossibleBy number, 3 call divideWhilePossibleBy number, 5
i = 7 numberSquared = sqr(number)
while i <= numberSquared for e = 0 to 6 step 2 while 1 i1 = i + e if number mod i1 <> 0 then exit while call printDivision number, i1 number = number / i1 numberSquared = sqr(number) wend next e i = i + 10 wend
if initialNumber = number then knownPrime = 1 end if end if
if knownPrime <> 0 then print initialNumber; " is prime" else print initialNumber; " is not prime" end if
print end sub
sub printDivision dividend, divisor print dividend; " / "; divisor; " = "; dividend / divisor end sub
' divide a number by other number while possible ' print division to screen sub divideWhilePossibleBy byref number, by while number mod by = 0 call printDivision number, by number = number / by wend end sub
When entering 3 or 2 it divided by that number just to say it's prime - this one says it right away! Tried to move as much of the repeating code to subroutines as I could, even if it is a single line - that's a single line I have to change, not 4 different ones in different places Got rid of gotos too, the only one I had second thoughts about was the [m4] one, kind of made sense at first, but then it was just a case of a condition that will eventually happen, so repeat ad infinitum and watch for the condition.
|
|