
Post by krzysztof on Aug 22, 2013 15:46:58 GMT 5
The following program looks in the range of integers <a,b> for all numbers divisible by any of the numbers from the given set of dividers. 'Determination of numbers divisible by any of dividers
input "Enter the lower limit of the range (a)" ; a input "Enter the upper limit of the range (b)" ; b input "Enter the number of dividers" ; n
for i = 1 to n input "Enter the " ; i ; " divider" ; P(i) next i
print "Subsequent numbers in the range <a,b> divisible by any of the given dividers: " ;
for i = a to b for j = 1 to n if i mod P(j) = 0 then print i ; ", "; exit for end if next j next i end



Post by krzysztof on Aug 26, 2013 5:35:49 GMT 5
Program that looks in the range of integers <a,b> for all numbers not divisible by any of the numbers from the given set of dividers. 'Determination of numbers not divisible by given dividers
input "Enter the lower limit of the range (a)" ; a input "Enter the upper limit of the range (b)" ; b input "Enter the number of dividers" ; n
for i = 1 to n input "Enter the " ; i ; " divider" ; P(i) next i
print "Subsequent numbers in the range <a,b> not divisible by any of the given dividers: " ;
for i = a to b divide = 0 for j = 1 to n if i mod P(j) = 0 then divide = 1 exit for end if next j if divide = 0 then print i ; ", "; end if next i print end



Post by krzysztof on Aug 27, 2013 5:13:39 GMT 5
The next code is an improvement of the first one, calculationg the numbers divisible by any of the given dividers. This problem can be solved by setting the numbers divisible by one of the divisors of the array P[n]. It is sufficient to note that these numbers are multiples of its divisors. Thus, for each divider must determine the minimum multiple within a range of <a,b>. Then choose the smallest multiples and send the output. If during the search for the smallest number we will get the one that has already been sent to the output, we will replace it at its next multiple. The operation will continue until the minimum number exceeds upper range "b". 'Determination of numbers divisible by any of dividers 'Improved code
input "Enter the lower limit of the range (a)" ; a input "Enter the upper limit of the range (b)" ; b input "Enter the number of dividers" ; n
dim P(n)
for i = 1 to n input "Enter the " ; i ; " divider" ; P(i)
if P(i) < 0 then P(i) = 1 * P(i) end if if a < 0 then V(i) = int(a / P(i)) * P(i) else V(i) = int((a + P(i)  1) / P(i)) * P(i) end if
next i
print "Subsequent numbers in the range <a,b> divisible by any of the given dividers: " ;
s = a  1
[loop] minv = 100 for i = 1 to n if V(i) = s then V(i) = V(i) + P(i) end if if V(i) < minv then minv = V(i) end if
next i
s = minv
if s > b then goto [end] end if
print s ; ", "; goto [loop]
[end] end



Post by krzysztof on Sept 2, 2013 12:25:42 GMT 5
Rosettacode has the example of code for calculating greatest common divisor using modulo expression, see rosettacode.org/wiki/Greatest_common_divisor#Run_BASICBut the simplest algorithm of GCD is based on subtraction of two numbers: input "Enter the first number" a ; a input "Enter the second number" b ; b
while a <> b if a < b then b = b  a else a = a  b end if wend
print "Greatest Common Divisor is " ; a
end



Post by meerkat on Sept 3, 2013 3:44:36 GMT 5
I tried 220 and 160 and it goes into a loop.



Post by krzysztof on Sept 3, 2013 9:03:49 GMT 5
I always use online version of RunBASIC available at www.runbasic.com to run my programs. Now I have checked my code again, and it works fine. For numbers 220 and 160 (also in opposite direction) result is 20. The code is based on Euclidean algorithm en.wikipedia.org/wiki/Euclidean_algorithmI tried to attach a file with screenshot of my calculation, but attachment does not work here.



Post by meerkat on Sept 3, 2013 9:19:12 GMT 5
Hmmm! Not sure why, but using the code mine still don't work. I used 220 (negative 220), and 160 and it goes into a tight loop.
Actually any negative number I try goes into a loop..



Post by krzysztof on Sept 3, 2013 9:34:30 GMT 5
I haven't noticed at first that you wrote about negative numbers, sorry. The code does not work with negative numbers. Both the Euclidean algorithm and the code work only with positive integers.



Post by krzysztof on Sept 5, 2013 4:00:00 GMT 5
The code for calculating relatively prime integers en.wikipedia.org/wiki/Coprime_integersinput "Enter the first limit" ; a input "Enter the second limit" ; b input "Enter the number p" ; p
print "Relatively prime integers to " ; p ; " are: " ;
for i = a to b ax = i bx = p while bx <> 0 t = bx bx = ax mod bx ax = t wend if ax = 1 then print i ; ", " ; end if
next i
end



Post by krzysztof on Sept 9, 2013 5:20:11 GMT 5
Integer square root 'Integer square root of "x"
input x a = 0 r1 = 1 r2 = 2 i = 0
while a <= x a = a + r1 r1 = r1 + r2 i = i + 1 wend
i = i  1
print "Integer square root of " ; x ; " = " ; i
end



Post by krzysztof on Sept 10, 2013 15:28:17 GMT 5
Prime factorization by trial division en.wikipedia.org/wiki/Prime_factor'Prime factorization by trial division
input "Enter the number:" ; p
print "Prime factors of " ; p ; " are: ";
for i = 2 to sqr(p)
while p mod i = 0 print i ; ", " ; p = p / i wend if p = 1 then exit for end if
next i
if p > 1 then print p ; end if
end

