Advertisement
hpolzer

Primfaktorzerlegung (bc)

Apr 16th, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. /*
  2. Compute the prime factors of a number using "bc", to run type "bc -q filename"
  3. A translation of my program in C, this one can handle large numbers.
  4. (Keep in mind that factoring large numbers may easily become infeasible...)
  5. Copyright (C) <April 13, 2015> Henning POLZER, h underscore polzer at gmx dot de
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21. define primfaktoren (z) {
  22. scale = 0
  23. s = z
  24. faktor = 2
  25. flag = 0
  26. exponent = 0
  27.  
  28. print z, " = "
  29. og = sqrt (z) + 1
  30. og /= 1 /* og abrunden */
  31.  
  32. if (z > 3)
  33. while (faktor < og + 1) {
  34. if (z % faktor == 0) {
  35. exponent += 1
  36. af = faktor
  37. if (exponent < 2) print faktor
  38. flag = 1
  39. z = z / faktor
  40. z /= 1 /* z abrunden */
  41. og = z
  42. } else {
  43. if (faktor % 2 != 0) faktor += 2 else faktor += 1
  44. exponent = 0
  45. }
  46. if ((z % faktor != 0) && (exponent > 1)) print "^", exponent
  47. if ((z % faktor == 0) && (faktor != af) && (flag == 1)) print " * "
  48. }
  49.  
  50. if (s == 1) print "1 * 1" else
  51. if (s == z) print "prim"
  52.  
  53. print "\n"
  54. }
  55.  
  56. /* 2^2 * 3^3 * 5^8 * 7^4 * 11^3 * 13^19 * 17^3 * 19^2 * 23^9 =
  57. 629625327458173519519741453570279519028974075767187500 */
  58.  
  59. primfaktoren (629625327458173519519741453570279519028974075767187500)
  60. quit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement