code_junkie

A Project Euler Puzzler (specifically in PHP)

Nov 11th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. function euler5(){
  2. $x = 20;
  3.  
  4. for ($y = 1; $y < 20; $y++) {
  5.  
  6. if (!($x%$y)) {
  7.  
  8. } else {
  9. $x+=20;
  10. $y = 1;
  11. }
  12.  
  13. }echo $x;
  14. };
  15.  
  16. <?php
  17. function gcd($a,$b) {
  18. while($a>0 && $b>0) {
  19. if($a>$b) $a=$a-$b; else $b=$b-$a;
  20. }
  21. if($a==0) return $b;
  22. return $a;
  23. }
  24. function euler5($i=20) {
  25. $euler=$x=1;
  26. while($x++<$i) {
  27. $euler*=$x/gcd($euler,$x);
  28. }
  29. return $euler;
  30. }
  31.  
  32. ?>
  33.  
  34. <?
  35. function eulerPuzzle()
  36. {
  37. $integers = array( 11,12,13,14,15,16,17,18,19 );
  38.  
  39. for ($n = 20; 1; $n += 20 ) {
  40. foreach ($integers as $int) {
  41. if ( $n % $int ) {
  42. break;
  43. }
  44. if ( $int == 19 ) {
  45. die ("Result:" . $n);
  46. }
  47. }
  48. }
  49. }
  50.  
  51. eulerPuzzle();
  52. ?>
  53.  
  54. <?php
  55. $i=20;
  56. while ($i+=20) {
  57. for ($j=19;$j!==10;--$j){
  58. if ($i%$j) continue 2;
  59. }
  60. die ("result: $in");
  61. }
  62.  
  63. puts 5*7*9*11*13*16*17*19
  64.  
  65. function eulerPuzzle() {
  66. $integers = array (11, 12, 13, 14, 15, 16, 17, 18, 19 );
  67.  
  68. for($n = 2520; 1; $n += 2520) {
  69. foreach ( $integers as $int ) {
  70. if ($n % $int) {
  71. break;
  72. }
  73. if ($int == 19) {
  74. die ( "Result:" . $n );
  75. }
  76. }
  77. }
  78. }
  79.  
  80. eulerPuzzle ();
  81.  
  82. #!/usr/bin/env python
  83.  
  84. from operator import mul
  85.  
  86. def factor(n):
  87. factors = {}
  88. i = 2
  89. while i < n and n != 1:
  90. while n % i == 0:
  91. try:
  92. factors[i] += 1
  93. except KeyError:
  94. factors[i] = 1
  95. n = n / i
  96. i += 1
  97. if n != 1:
  98. factors[n] = 1
  99. return factors
  100.  
  101. base = {}
  102. for i in range(2, 2000):
  103. for f, n in factor(i).items():
  104. try:
  105. base[f] = max(base[f], n)
  106. except KeyError:
  107. base[f] = n
  108.  
  109. print reduce(mul, [f**n for f, n in base.items()], 1)
Add Comment
Please, Sign In to add comment