Author Topic: Factorials, Powers & Multiplication  (Read 3882 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Factorials, Powers & Multiplication
« on: June 28, 2019, 03:28:16 am »
Hello everyone!

I developed a program which
1. Can give you factorial of any positive integer.  For example 1000!
2. Raise a positive integer to any positive integer. For example 2^2000
3. Can give the product of any positive integer. For example 896346326469864398643969869463294269846946329423986*78343664782643876748367483676438


Steps to setup the program -
1. Paste the code in QB64 IDE and save it with the name "fpm.bas"
2. Open the commandline and start executing commands in it.
3. Try "fpm -help" for more info.


Screenshot_1.png

Code: QB64: [Select]
  1. '#####################################
  2. '#                                   #
  3. '#      FPM by Ashish                #
  4. '#                                   #
  5. '#####################################
  6. '28 Jun, 2019
  7. 'Save the file with name "fpm.bas" and then compile to produce fpm.exe/fpm.
  8. 'and then use commandline interface for executing commands in it.
  9.  
  10.  
  11. a$ = COMMAND$(1)
  12. _CONSOLETITLE "Factorials, Power and Multiplication"
  13.  
  14. IF _TRIM$(a$) = "-help" THEN
  15.     _ECHO "--------------------------------------"
  16.     _ECHO "*          FPM by Ashish             *"
  17.     _ECHO "--------------------------------------"
  18.     _ECHO ""
  19.     _ECHO " Usage - fpm [function_name]([arguements])"
  20.     _ECHO "List of arguements -"
  21.     _ECHO "1. mult()"
  22.     _ECHO "Syntax - mult(a,b) | where a,b are positive integers of any digit."
  23.     _ECHO "Returns a*b"
  24.     _ECHO "Example : Try fpm mult(2,7)"
  25.     _ECHO ""
  26.     _ECHO "2. pow()"
  27.     _ECHO "Syntax - pow(a,b) |where a,b are positive integers of any digit."
  28.     _ECHO "Returns a^b"
  29.     _ECHO "Example : Try fpm pow(3,2)"
  30.     _ECHO ""
  31.     _ECHO "3. fact()"
  32.     _ECHO "Syntax - fact(a) | where a is positive integer of any digit."
  33.     _ECHO "Returns a! , that is 1*2*3*...*(a-1)*a"
  34.     _ECHO "Example : Try fpm fact(10)"
  35.  
  36.     SYSTEM
  37. a$ = _TRIM$(a$)
  38. IF RIGHT$(a$, 1) <> ")" THEN a$ = ""
  39.  
  40.     CASE "mult"
  41.         FOR i = 1 TO LEN(a$)
  42.             IF MID$(a$, i, 1) = "," THEN EXIT FOR
  43.         NEXT
  44.         t# = TIMER
  45.         _ECHO mult$(MID$(a$, 6, i - 6), MID$(a$, i + 1, LEN(a$) - (i + 1)))
  46.         _ECHO "Time taken : " + STR$(TIMER - t#) + " seconds"
  47.     CASE "fact"
  48.         n~&& = VAL(MID$(a$, 6, LEN(a$) - 6))
  49.         IF n~&& = 0 OR n~&& = 1 THEN _ECHO "1": SYSTEM
  50.         IF n~&& = 2 THEN _ECHO "2": SYSTEM
  51.         c$ = "1"
  52.         IF n~&& > 500 THEN _ECHO "Can take " + STR$(2 ^ ((n~&& / 500) + 1)) + " seconds or more."
  53.         t# = TIMER
  54.         FOR i~&& = 2 TO n~&&
  55.             c$ = mult(c$, _TRIM$(STR$(i~&&)))
  56.         NEXT
  57.         _ECHO c$
  58.         _ECHO "Time taken : " + STR$(TIMER - t#) + " seconds"
  59.     CASE "pow("
  60.         FOR i = 1 TO LEN(a$)
  61.             IF MID$(a$, i, 1) = "," THEN EXIT FOR
  62.         NEXT
  63.         b$ = MID$(a$, 5, i - 5)
  64.         c$ = MID$(a$, i + 1, LEN(a$) - (i + 1))
  65.         c~&& = VAL(c$)
  66.         IF VAL(b$) = 0 AND c~&& = 0 THEN _ECHO "not defined.": SYSTEM
  67.         IF c~&& = 0 THEN _ECHO "1": SYSTEM
  68.         IF c~&& = 1 THEN _ECHO b$: SYSTEM
  69.         d$ = b$
  70.         t# = TIMER
  71.         FOR i~&& = 1 TO c~&& - 1
  72.             d$ = mult(d$, b$)
  73.         NEXT
  74.         _ECHO d$
  75.         _ECHO "Time taken : " + STR$((TIMER - t#)) + " seconds"
  76.     CASE ELSE
  77.         COLOR _RGB(255, 0, 0)
  78.         _ECHO ""
  79.         _ECHO "Syntax error."
  80.         _ECHO "Try fpm -help"
  81.         COLOR _RGB(255, 255, 255)
  82.  
  83. FUNCTION mult$ (n1$, n2$)
  84.     IF VAL(n1$) = 0 OR VAL(n2$) = 0 THEN mult$ = "0": EXIT FUNCTION
  85.     IF LEN(n1$) = 1 THEN mult$ = mult_single_dgt$(n2$, n1$): EXIT FUNCTION
  86.     IF LEN(n2$) = 1 THEN mult$ = mult_single_dgt$(n1$, n2$): EXIT FUNCTION
  87.     DIM res$(LEN(n2$))
  88.     FOR i = LEN(n2$) TO 1 STEP -1
  89.         dgt$1 = MID$(n2$, i, 1)
  90.         res$(LEN(n2$) - i) = mult_single_dgt(n1$, dgt$1) + STRING$(LEN(n2$) - i, "0")
  91.         p1 = LEN(res$(LEN(n2$) - i))
  92.         IF p1 > p0 THEN g = p1
  93.         p0 = p1
  94.     NEXT
  95.     FOR i = 0 TO LEN(n2$) - 1
  96.         IF LEN(res$(i)) < g THEN res$(i) = STRING$(g - LEN(res$(i)), "0") + res$(i)
  97.         ' _echo res$(i)
  98.     NEXT
  99.     FOR i = g TO 1 STEP -1
  100.         sum = 0
  101.         FOR j = 0 TO UBOUND(res$) - 1
  102.             sum = sum + VAL(MID$(res$(j), i, 1))
  103.         NEXT
  104.         ' ? sum
  105.         str_sum$ = _TRIM$(STR$(sum + carry))
  106.         IF i > 1 THEN
  107.             IF LEN(str_sum$) > 1 THEN
  108.                 final_dgt$ = RIGHT$(str_sum$, 1)
  109.                 carry = VAL(LEFT$(str_sum$, LEN(str_sum$) - 1))
  110.             ELSE
  111.                 final_dgt$ = str_sum$
  112.                 carry = 0
  113.             END IF
  114.         ELSE
  115.             final_dgt$ = str_sum$
  116.             IF LEN(final_dgt$) > 1 THEN
  117.                 temp$ = ""
  118.                 FOR j = LEN(final_dgt$) TO 1 STEP -1
  119.                     temp$ = temp$ + MID$(final_dgt$, j, 1)
  120.                 NEXT
  121.                 final_dgt$ = temp$
  122.             END IF
  123.         END IF
  124.         final_sum$ = final_sum$ + final_dgt$
  125.     NEXT
  126.     FOR i = LEN(final_sum$) TO 1 STEP -1
  127.         mult$ = mult$ + MID$(final_sum$, i, 1)
  128.     NEXT
  129.  
  130. FUNCTION mult_single_dgt$ (n$, d$)
  131.     IF d$ = "0" THEN mult_single_dgt$ = "0": EXIT FUNCTION
  132.     IF d$ = "1" THEN mult_single_dgt$ = n$: EXIT FUNCTION
  133.     IF n$ = "1" THEN mult_single_dgt$ = d$: EXIT FUNCTION
  134.     FOR j = LEN(n$) TO 1 STEP -1
  135.         dgt_1$1 = MID$(n$, j, 1)
  136.         r = (VAL(dgt_1$1) * VAL(d$)) + carry
  137.         ' ? r
  138.         IF j > 1 THEN
  139.             FOR k = 90 TO 0 STEP -10
  140.                 IF r >= k THEN carry = k * 0.1: final$ = _TRIM$(STR$(r - k)): EXIT FOR
  141.             NEXT
  142.         ELSE
  143.             final$ = _TRIM$(STR$(r))
  144.             IF LEN(n$) > 1 THEN
  145.                 tmp$ = ""
  146.                 FOR k = LEN(final$) TO 1 STEP -1
  147.                     tmp$ = tmp$ + MID$(final$, k, 1)
  148.                 NEXT
  149.                 final$ = tmp$
  150.             END IF
  151.         END IF
  152.         product$ = product$ + final$
  153.     NEXT
  154.     IF LEN(n$) > 1 THEN
  155.         FOR j = LEN(product$) TO 1 STEP -1
  156.             mult_single_dgt$ = mult_single_dgt$ + MID$(product$, j, 1)
  157.         NEXT
  158.     ELSE mult_single_dgt$ = product$
  159.     END IF
  160.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Factorials, Powers & Multiplication
« Reply #1 on: June 28, 2019, 11:30:40 am »
Hi Ashish,

Wow, you have powers going too. This will be handy, thanks!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Factorials, Powers & Multiplication
« Reply #2 on: June 28, 2019, 11:41:20 am »
Thanks Bplus!

NEW UPDATE :
1. Fixed some glitches and optimized the code.
2. Now, you can use nested functions!! Yes, you read it right. For example, you can execute command like this "fpm mult(pow(2,4),mult(3,3))"
You can nest it even more.

Screenshot_1.png

Code: QB64: [Select]
  1. '#####################################
  2. '#                                   #
  3. '#      FPM by Ashish                #
  4. '#                                   #
  5. '#####################################
  6. '28 Jun, 2019
  7. 'Save the file with name "fpm.bas" and then compile to produce fpm.exe/fpm.
  8. 'and then use commandline interface for executing commands in it.
  9.  
  10.  
  11. a$ = COMMAND$(1)
  12. _CONSOLETITLE "Factorials, Power and Multiplication"
  13.  
  14. IF _TRIM$(a$) = "-help" THEN
  15.     _ECHO "--------------------------------------"
  16.     _ECHO "*          FPM by Ashish             *"
  17.     _ECHO "--------------------------------------"
  18.     _ECHO ""
  19.     _ECHO " Usage - fpm [function_name]([arguements])"
  20.     _ECHO "List of arguements -"
  21.     _ECHO "1. mult()"
  22.     _ECHO "Syntax - mult(a,b) | where a,b are positive integers of any digit."
  23.     _ECHO "Returns a*b"
  24.     _ECHO "Example : Try fpm mult(2,7)"
  25.     _ECHO ""
  26.     _ECHO "2. pow()"
  27.     _ECHO "Syntax - pow(a,b) |where a,b are positive integers of any digit."
  28.     _ECHO "Returns a^b"
  29.     _ECHO "Example : Try fpm pow(3,2)"
  30.     _ECHO ""
  31.     _ECHO "3. fact()"
  32.     _ECHO "Syntax - fact(a) | where a is positive integer of any digit."
  33.     _ECHO "Returns a! , that is 1*2*3*...*(a-1)*a"
  34.     _ECHO "Example : Try fpm fact(10)"
  35.     _ECHO "Note : These function can also be used in the place of a and b"
  36.     SYSTEM
  37. dummy$ = execute$(a$, 1)
  38.  
  39. FUNCTION execute$ (a$, sts)
  40.     a$ = _TRIM$(a$)
  41.     IF RIGHT$(a$, 1) <> ")" AND sts = 1 THEN a$ = ""
  42.  
  43.     SELECT CASE LEFT$(a$, 4)
  44.         CASE "mult"
  45.             brc = 0
  46.             FOR i = 1 TO LEN(a$)
  47.                 IF MID$(a$, i, 1) = "," AND brc = 1 THEN EXIT FOR
  48.                 IF MID$(a$, i, 1) = "(" THEN brc = brc + 1
  49.                 IF MID$(a$, i, 1) = ")" THEN brc = brc - 1
  50.             NEXT
  51.             IF sts = 1 THEN t# = TIMER
  52.             num_a$ = _TRIM$(MID$(a$, 6, i - 6))
  53.             num_b$ = _TRIM$(MID$(a$, i + 1, LEN(a$) - (i + 1)))
  54.             IF sts = 1 THEN
  55.                 _ECHO mult$(execute$(num_a$, 0), execute$(num_b$, 0))
  56.                 _ECHO "Time taken : " + STR$(TIMER - t#) + " seconds"
  57.             ELSE
  58.                 execute$ = mult$(execute$(num_a$, 0), execute$(num_b$, 0))
  59.             END IF
  60.         CASE "fact"
  61.             n~&& = VAL(execute$(MID$(a$, 6, LEN(a$) - 6), 0))
  62.             IF sts = 1 THEN
  63.                 IF n~&& = 0 OR n~&& = 1 THEN _ECHO "1": SYSTEM
  64.                 IF n~&& = 2 THEN _ECHO "2": SYSTEM
  65.             ELSE
  66.                 IF n~&& = 0 OR n~&& = 1 THEN execute$ = "1": EXIT FUNCTION
  67.                 IF n~&& = 2 THEN execute$ = "2": EXIT FUNCTION
  68.             END IF
  69.             c$ = "1"
  70.             IF sts = 1 THEN
  71.                 IF n~&& > 500 THEN _ECHO "Can take " + STR$(2 ^ ((n~&& / 500) + 1)) + " seconds or more."
  72.                 t# = TIMER
  73.             END IF
  74.             FOR i~&& = 2 TO n~&&
  75.                 c$ = mult(c$, _TRIM$(STR$(i~&&)))
  76.             NEXT
  77.             IF sts = 1 THEN
  78.                 _ECHO c$
  79.                 _ECHO "Time taken : " + STR$(TIMER - t#) + " seconds"
  80.             ELSE execute$ = c$
  81.             END IF
  82.         CASE "pow("
  83.             brc = 0
  84.             FOR i = 1 TO LEN(a$)
  85.                 IF MID$(a$, i, 1) = "," AND brc = 1 THEN EXIT FOR
  86.                 IF MID$(a$, i, 1) = "(" THEN brc = brc + 1
  87.                 IF MID$(a$, i, 1) = ")" THEN brc = brc - 1
  88.             NEXT
  89.             b$ = execute$(_TRIM$(MID$(a$, 5, i - 5)), 0)
  90.             c$ = execute$(_TRIM$(MID$(a$, i + 1, LEN(a$) - (i + 1))), 0)
  91.        
  92.             c~&& = VAL(c$)
  93.             IF sts = 1 THEN
  94.                 IF VAL(b$) = 0 AND c~&& = 0 THEN _ECHO "not defined.": SYSTEM
  95.                 IF c~&& = 0 THEN _ECHO "1": SYSTEM
  96.                 IF c~&& = 1 THEN _ECHO b$: SYSTEM
  97.             ELSE
  98.                 IF VAL(b$) = 0 AND c~&& = 0 THEN _ECHO "not defined.": SYSTEM
  99.                 IF c~&& = 0 THEN execute$ = "1": EXIT FUNCTION
  100.                 IF c~&& = 1 THEN execute$ = b$: EXIT FUNCTION
  101.             END IF
  102.             d$ = b$
  103.  
  104.             IF sts = 1 THEN t# = TIMER
  105.             FOR i~&& = 1 TO c~&& - 1
  106.                 d$ = mult(d$, b$)
  107.             NEXT
  108.             IF sts = 1 THEN
  109.                 _ECHO d$
  110.                 _ECHO "Time taken : " + STR$((TIMER - t#)) + " seconds"
  111.             ELSE
  112.                 execute$ = d$
  113.             END IF
  114.         CASE ELSE
  115.             IF sts = 1 THEN
  116.                 _ECHO ""
  117.                 _ECHO "Syntax error."
  118.                 _ECHO "Try fpm -help"
  119.             ELSE execute$ = a$
  120.             END IF
  121.     END SELECT
  122.  
  123. FUNCTION mult$ (n1$, n2$)
  124.     IF VAL(n1$) = 0 OR VAL(n2$) = 0 THEN mult$ = "0": EXIT FUNCTION
  125.     IF LEN(n1$) = 1 THEN mult$ = mult_single_dgt$(n2$, n1$): EXIT FUNCTION
  126.     IF LEN(n2$) = 1 THEN mult$ = mult_single_dgt$(n1$, n2$): EXIT FUNCTION
  127.     DIM res$(LEN(n2$))
  128.     FOR i = LEN(n2$) TO 1 STEP -1
  129.         dgt$1 = MID$(n2$, i, 1)
  130.         res$(LEN(n2$) - i) = mult_single_dgt(n1$, dgt$1) + STRING$(LEN(n2$) - i, "0")
  131.         p1 = LEN(res$(LEN(n2$) - i))
  132.         IF p1 > p0 THEN g = p1
  133.         p0 = p1
  134.     NEXT
  135.     FOR i = 0 TO LEN(n2$) - 1
  136.         IF LEN(res$(i)) < g THEN res$(i) = STRING$(g - LEN(res$(i)), "0") + res$(i)
  137.         ' _echo res$(i)
  138.     NEXT
  139.     FOR i = g TO 1 STEP -1
  140.         sum = 0
  141.         FOR j = 0 TO UBOUND(res$) - 1
  142.             sum = sum + VAL(MID$(res$(j), i, 1))
  143.         NEXT
  144.         ' ? sum
  145.         str_sum$ = _TRIM$(STR$(sum + carry))
  146.         IF i > 1 THEN
  147.             IF LEN(str_sum$) > 1 THEN
  148.                 final_dgt$ = RIGHT$(str_sum$, 1)
  149.                 carry = VAL(LEFT$(str_sum$, LEN(str_sum$) - 1))
  150.             ELSE
  151.                 final_dgt$ = str_sum$
  152.                 carry = 0
  153.             END IF
  154.         ELSE
  155.             final_dgt$ = str_sum$
  156.             IF LEN(final_dgt$) > 1 THEN
  157.                 temp$ = ""
  158.                 FOR j = LEN(final_dgt$) TO 1 STEP -1
  159.                     temp$ = temp$ + MID$(final_dgt$, j, 1)
  160.                 NEXT
  161.                 final_dgt$ = temp$
  162.             END IF
  163.         END IF
  164.         final_sum$ = final_sum$ + final_dgt$
  165.     NEXT
  166.     FOR i = LEN(final_sum$) TO 1 STEP -1
  167.         mult$ = mult$ + MID$(final_sum$, i, 1)
  168.     NEXT
  169.  
  170. FUNCTION mult_single_dgt$ (n$, d$)
  171.     IF d$ = "0" THEN mult_single_dgt$ = "0": EXIT FUNCTION
  172.     IF d$ = "1" THEN mult_single_dgt$ = n$: EXIT FUNCTION
  173.     IF n$ = "1" THEN mult_single_dgt$ = d$: EXIT FUNCTION
  174.     FOR j = LEN(n$) TO 1 STEP -1
  175.         dgt_1$1 = MID$(n$, j, 1)
  176.         r = (VAL(dgt_1$1) * VAL(d$)) + carry
  177.         ' ? r
  178.         IF j > 1 THEN
  179.             FOR k = 90 TO 0 STEP -10
  180.                 IF r >= k THEN carry = k * 0.1: final$ = _TRIM$(STR$(r - k)): EXIT FOR
  181.             NEXT
  182.         ELSE
  183.             final$ = _TRIM$(STR$(r))
  184.             IF LEN(n$) > 1 THEN
  185.                 tmp$ = ""
  186.                 FOR k = LEN(final$) TO 1 STEP -1
  187.                     tmp$ = tmp$ + MID$(final$, k, 1)
  188.                 NEXT
  189.                 final$ = tmp$
  190.             END IF
  191.         END IF
  192.         product$ = product$ + final$
  193.     NEXT
  194.     IF LEN(n$) > 1 THEN
  195.         FOR j = LEN(product$) TO 1 STEP -1
  196.             mult_single_dgt$ = mult_single_dgt$ + MID$(product$, j, 1)
  197.         NEXT
  198.     ELSE mult_single_dgt$ = product$
  199.     END IF
  200.  
* fpm.bas (Filesize: 7.36 KB, Downloads: 217)
« Last Edit: June 28, 2019, 11:50:31 am by Ashish »
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials