|
Post by krzysztof on Aug 18, 2013 17:45:27 GMT -5
I added a command to write calculated prime numbers to a text file, and it works almost fine. But there is a problem: the prime numbers are correctly calculated and stored in a file up certain values. But when I enter a value of 1260, instead od numbers in the file there are such weird symbols:" 㐠″㜴㔠″㤵㘠‱㜶㜠‱㌷㜠‹㌸㠠‹㜹ㄠㄠ㌰ㄠ㜰ㄠ㤰ㄠ㌱ㄠ㜲ㄠㄳㄠ㜳". These symbols are in the generated file (see inside the file), but the prime numbers are correctly displayed on the screen. This is not normal in my opinion. I used the downloadable, free version of Run Basic. input "Enter the limit"; n
open "c:\Prime_numbers.txt" for output as #file
for p = 2 to n divide = 0 for q = 2 to Sqr(p) if p / q = int(p / q) then divide = 1 exit for end if next q if divide = 0 then print #file, p; " "; end If
next p close #file
print "Ready. Calculated prime numbers are stored in c:\Prime_numbers.txt file"
open "c:\Prime_numbers.txt" for input as #file input #file, p$ print p$ close #file
end
|
|
|
Post by StefanPendl on Aug 20, 2013 7:12:46 GMT -5
The problem is that Windows Notepad thinks this is an Unicode encoded file. Notepad++ is correctly identifying it as a ANSI encoded file.
If you place the space before the number, even Notepad correctly identifies the encoding of the file.
print #file, " "; p;
|
|
|
Post by krzysztof on Aug 20, 2013 9:06:46 GMT -5
The content of file with weird symbols is properly viewed using WordPad. But the problem is corrected using Your code. Many thanks for the help.
|
|
|
Post by krzysztof on Sept 13, 2013 10:18:41 GMT -5
Here is the code for primality check by trial divisions. It works fine only for special numbers, for other does not. For instance try these numbers: 11 - prime, ok 22 - complex, ok 33 - error: "Runtime Error: exit for. No loop to EXIT" 44 - complex, ok 55 - error: "Runtime Error: exit for. No loop to EXIT" The errors are only using RunBASIC, on JustBASIC everything is fine. input "Enter the number to check primality: " ; p
divide = 0
if p > 2 then if p mod 2 = 0 then divide = 1 else for i = 3 to sqr(p) step 2 if p mod i = 0 then divide = 1 exit for end if next i end if end if
if divide = 0 then print "Number " ; p ; " is prime" else print "Number " ; p ; " is complex" end if
end Why is this happening?
|
|
|
Post by krzysztof on Sept 15, 2013 4:48:39 GMT -5
Mayby it is just a bug in RunBASIC. The code works perfectly in JustBASIC and other dialects of BASIC. For me there is no reason why, for instance, number 32 works fine, giving the message "Number 32 is complex", but 33 crashed the code with "Runtime Error: exit for No loop to EXIT" warning. Number 34 also works fine, but 35 does not. 36, 36, 37 and 38 fine, but 39 also crushes. Weird, isn't it?
|
|
|
Post by krzysztof on Sept 15, 2013 14:36:55 GMT -5
Yes, it is a bug, but in online version of RunBASIC. The problem is with executing "step" expression in my code. During running the code using online version of RunBASIC I always receive the earlier mentioned error. But when I downloaded and installed free version of RB, my code works fine. You can try another code to prove that bug in online version. Copy the code for x = 1 to 20 y=x*3 if y>40 then EXIT FOR next x print "Final x value ";x print "Final y value ";y from page www.runbasic.com/docs/for_statement.html, section of "EXIT FOR". Then add "step 1" expression to the first line of code to obtain "for x = 1 to 20 step 1". Run that modified code in online version of RunBASIC - it will crush with "Runtime Error: EXIT FOR No loop to EXIT" error. But when You will run it using downloaded free version of RB, everything will be fine. The code will work correctly.
|
|