Stegano/stegano/lsbset/generators.py
2017-03-09 10:02:52 +01:00

141 lines
3.7 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Stéganô - Stéganô is a basic Python Steganography module.
# Copyright (C) 2010-2017 Cédric Bonhomme - https://www.cedricbonhomme.org
#
# For more information : https://github.com/cedricbonhomme/Stegano
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.2 $"
__date__ = "$Date: 2011/12/28 $"
__revision__ = "$Date: 2012/12/14 $"
__license__ = "GPLv3"
import math
import itertools
from typing import Iterator
def identity() -> Iterator[int]:
"""f(x) = x
"""
n = 0
while True:
yield n
n += 1
def triangular_numbers() -> Iterator[int]:
"""http://oeis.org/A000217
Triangular numbers: a(n) = C(n+1,2) = n(n+1)/2 = 0+1+2+...+n.
"""
n = 0
while True:
yield (n*(n+1))//2
n += 1
def fermat() -> Iterator[int]:
"""https://oeis.org/A000215
Generate the n-th Fermat Number.
"""
y = 3
while True:
yield y
y = pow(y-1,2)+1
def mersenne() -> Iterator[int]:
"""https://oeis.org/A001348
Generate 2^n - 1.
"""
y = 3
while True:
yield y
y = 2*y + 1
def eratosthenes() -> Iterator[int]:
"""https://oeis.org/A000040
Generate the prime numbers with the sieve of Eratosthenes.
"""
d = {} # type: dict[int, int]
for i in itertools.count(2):
if i in d:
for j in d[i]:
d[i + j] = d.get(i + j, []) + [j]
del d[i]
else:
d[i * i] = [i]
yield i
def composite() -> Iterator[int]:
"""https://oeis.org/A002808
Generate the composite numbers using the sieve of Eratosthenes.
"""
p1 = 3
for p2 in eratosthenes():
for n in range(p1 + 1, p2):
yield n
p1 = p2
def carmichael() -> Iterator[int]:
"""https://oeis.org/A002997
Composite numbers n such that a^(n-1) == 1 (mod n) for every a coprime to n.
"""
for m in eratosthenes_composite():
for a in range(2, m):
if pow(a,m,m) != a:
break
else:
yield m
def ackermann(m: int, n: int) -> int:
"""Ackermann number.
"""
if m == 0:
return n + 1
elif n == 0:
return ackermann(m - 1, 1)
else:
return ackermann(m - 1, ackermann(m, n - 1))
def fibonacci() -> Iterator[int]:
"""https://oeis.org/A000045
A generator for Fibonacci numbers, goes to next number in series on each call.
This generator start at 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, ...
"""
a, b = 1, 2
while True:
yield a
a, b = b, a + b
def syracuse(l: int = 15) -> Iterator[int]:
"""Generate the sequence of Syracuse.
"""
y = l
while True:
yield y
q,r = divmod(y,2)
if r == 0:
y = q
else:
y = 3*y + 1
def log_gen() -> Iterator[int]:
"""Logarithmic generator.
"""
y = 1
while True:
adder = max(1, math.pow(10, int(math.log10(y))))
yield int(y)
y = y + int(adder)