Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Created March 31, 2020 05:58
Show Gist options
  • Save harrisonmalone/3526433ab2765a0121478aa2b3651870 to your computer and use it in GitHub Desktop.
Save harrisonmalone/3526433ab2765a0121478aa2b3651870 to your computer and use it in GitHub Desktop.
prime numbers and swapping things in arrays
def swap_first_items(arr)
copy = arr[0..arr.length]
i = 0
while i < arr.size - 1
if arr[i] > arr[i + 1]
arr[i] = copy[i + 1]
arr[i + 1] = copy[i]
break
end
i += 1
end
return arr
end
# p swap_first_items([5, 22, 29, 39, 19, 51, 78, 96, 84])
def generate_first_100_prime_numbers
num = 1
primes = []
100.times do
divisor = 2
num.times do
if num == divisor
primes << num
end
if num % divisor == 0
break
end
divisor += 1
end
num += 1
end
return primes
end
# p generate_first_100_prime_numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment