Skip to content

Instantly share code, notes, and snippets.

View Lucas-Developer's full-sized avatar

Lucas Lucas-Developer

View GitHub Profile
@Lucas-Developer
Lucas-Developer / bitcoin_address_validation.py
Created May 26, 2018 01:42
Bitcoin/address validation
from hashlib import sha256
digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
def decode_base58(bc, length):
n = 0
for char in bc:
n = n * 58 + digits58.index(char)
return n.to_bytes(length, 'big')
def check_bc(bc):
from fractions import Fraction as Fr
def bernoulli(n):
A = [0] * (n+1)
for m in range(n+1):
A[m] = Fr(1, m+1)
for j in range(m, 0, -1):
A[j-1] = j*(A[j-1] - A[j])
return A[0] # (which is Bn)
@Lucas-Developer
Lucas-Developer / e.py
Created May 12, 2018 01:42
Número de Euler
'''
número euler calculado limite de (1+1/n)^n com n se aproximando ao
infinito calculando os 200 primeiros dígitos, através da
biblioteca decimal
'''
from decimal import Decimal, getcontext
def euler(precisao):
prec_n = precisao
@Lucas-Developer
Lucas-Developer / conjectura_de_agoh–giuga.py
Created May 11, 2018 22:59
Conjectura de Agoh–Giuga
listnumbers = range(1,500)
for i in listnumbers:
sum = 0
value = 1
while value <= i-1:
sum+=value**(i-1)
value+=1
if ((sum%i)+1)%i == 0:
print("%d é primo pela conjectura de Agoh-Giuga!"%(i))
@Lucas-Developer
Lucas-Developer / matrix_multiplication.rs
Created April 29, 2018 19:54
Multiply two matrices together. They can be of any dimensions, so long as the number of columns of the first matrix is equal to the number of rows of the second matrix.
struct Matrix {
dat: [[f32; 3]; 3]
}
impl Matrix {
pub fn mult_m(a: Matrix, b: Matrix) -> Matrix
{
let mut out = Matrix {
dat: [[0., 0., 0.],
[0., 0., 0.],
@Lucas-Developer
Lucas-Developer / factorization.py
Created April 29, 2018 19:47
Em teoria dos números, o problema da fatoração/ fatoração de inteiros consiste em encontrar um divisor não trivial de um número composto; Por exemplo dado o número 91, o objetivo é encontrar um número tal como 7 que o divida. Se esses inteiros estão restritos a números primos, este processo é chamado de fatoração por números primos/fatoração prima.
def factors(n):
return [i for i in range(1, n//2 + 1) if not n%i] + [n]
print(factors(45))
@Lucas-Developer
Lucas-Developer / crivo_de_eratóstenes.py
Created April 29, 2018 19:32
O Crivo de Eratóstenes é um algoritmo e um método simples e prático para encontrar números primos até um certo valor limite. Segundo a tradição, foi criado pelo matemático grego Eratóstenes (a.c. 285-194 a.C.), o terceiro bibliotecário-chefe da Biblioteca de Alexandria.
def eratosthenes2(n):
multiples = set()
for i in range(2, n+1):
if i not in multiples:
yield i
multiples.update(range(i*i, n+1, i))
print(list(eratosthenes2(100)))
@Lucas-Developer
Lucas-Developer / knight's_tour.rs
Created April 29, 2018 19:17
A knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every square only once. If the knight ends on a square that is one knight's move from the beginning square, the tour is closed, otherwise it is open.
use std::fmt;
const SIZE: usize = 8;
const MOVES: [(i32, i32); 8] = [(2,1), (1,2), (-1,2), (-2,1), (-2,-1), (-1,-2), (1,-2), (2,-1)];
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
struct Point {
x: i32,
y: i32
}
@Lucas-Developer
Lucas-Developer / n-queens_problem.rs
Created April 29, 2018 19:09
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
const N: usize = 8;
fn try(mut board: &mut [[bool; N]; N], row: usize, mut count: &mut i64) {
if row == N {
*count += 1;
for r in board.iter() {
println!("{}", r.iter().map(|&x| if x {"x"} else {"."}.to_string()).collect::<Vec<String>>().join(" "))
}
println!("");
return
@Lucas-Developer
Lucas-Developer / lucas-lehmer_test.py
Created April 29, 2018 18:33
In mathematics, the Lucas–Lehmer test is a primality test for Mersenne numbers. The test was originally developed by Édouard Lucas in 1856 and subsequently improved by Lucas in 1878 and Derrick Henry Lehmer in the 1930s.
def isqrt(n):
if n < 0:
raise ValueError
elif n < 2:
return n
else:
a = 1 << ((1 + n.bit_length()) >> 1)
while True:
b = (a + n // a) >> 1
if b >= a: