10 REM Prime Factors Kata in BASIC 20 REM by Peter Kofler, www.code-cop.org 30 REM (C) 2010, Peter Kofler, licensed under BSD License. 80 GOTO 1000 90 REM ***** test infrastructure ***** 100 REM ----- method assert equals(int) 110 REM in ... me$ ... message 120 REM in ... ex ... expected 130 REM in ... ac ... actual 140 IF ex=ac THEN RETURN 150 PRINT "{red}red{blue}" 160 PRINT me$;" expected";ex;" was";ac 170 STOP 180 RETURN 200 REM ----- method teardown 210 REM in ... du ... start timestamp 220 PRINT "{green}green{blue} (";ti-du;"ticks)" 230 RETURN 300 REM ----- method setup 310 REM in ... me$ ... test name 320 REM out ... du ... start timestamp 330 PRINT "test ";me$; LEFT$(" ",10- LEN(me$)); 340 du=ti 350 RETURN 400 REM ----- method assert prime factors 410 READ me$ 420 GOSUB 300 430 READ i 440 GOSUB 9000 450 READ af 460 ex=af : ac=pf(0) : me$="num factors" : GOSUB 100 470 IF af=0 THEN GOTO 520 480 FOR j=1 TO af 490 READ ex 500 ac=pf(j) : me$= STR$(j)+". factor" : GOSUB 100 510 NEXT 520 GOSUB 200 530 RETURN 990 REM ***** test cases ***** 1000 DATA "one", 1, 0 1010 GOSUB 400 1100 DATA "two", 2, 1, 2 1110 GOSUB 400 1200 DATA "three", 3, 1, 3 1210 GOSUB 400 1300 DATA "four", 4, 2, 2, 2 1310 GOSUB 400 1400 DATA "five", 6, 2, 2, 3 1410 GOSUB 400 1500 DATA "six", 8, 3, 2, 2, 2 1510 GOSUB 400 1600 DATA "seven", 9, 2, 3, 3 1610 GOSUB 400 1700 DATA "eight", 10, 2, 2, 5 1710 GOSUB 400 1800 DATA "large", 32719, 1, 32719 1810 GOSUB 400 1900 me$="huge" : GOSUB 300 1910 i=2^31-1 1920 GOSUB 9000 1930 ex=1 : ac=pf(0) : me$="num factors" : GOSUB 100 1940 ex=2^31-1 : ac=pf(1) : me$="1st factor" : GOSUB 100 1950 GOSUB 200 8990 END 9000 REM ----- function generate 9010 REM in ... i ... number 9020 REM out ... pf() ... factors 9030 REM mod ... ca ... pf candidate 9040 pf(0)=0 : ca=2 : REM special case 9050 IF i=1 THEN RETURN 9060 IF INT(i/ca)*ca=i THEN GOSUB 9200 : GOTO 9050 9070 FOR ca=3 TO INT( SQR(i)) STEP 2 9080 IF i=1 THEN RETURN 9090 IF INT(i/ca)*ca=i THEN GOSUB 9200 : GOTO 9080 9100 NEXT 9110 IF i>1 THEN ca=i : GOSUB 9200 9120 RETURN 9200 pf(0)=pf(0)+1 9210 pf(pf(0))=ca 9220 i=i/ca 9230 RETURN