{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import Data.Word\n", "import Data.Bits" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "a :: Word8\n", "a=156" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "b :: Word16\n", "b = 202" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Redundant bracket
Found:
(fromIntegral a) * b
Why Not:
fromIntegral a * b
" ], "text/plain": [ "Line 1: Redundant bracket\n", "Found:\n", "(fromIntegral a) * b\n", "Why not:\n", "fromIntegral a * b" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "31512" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "(fromIntegral a) * b" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "24" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "(156 * 202) `mod` 256" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generator A starts with 873\n", "\n", "Generator B starts with 583" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "generator :: Word64 -> Word64 -> Word64 -> Word64\n", "generator divisor factor n = fromIntegral $ fromIntegral n * factor `rem` divisor" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "generatorA = generator 2147483647 16807\n", "generatorB = generator 2147483647 48271" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "toWord16 :: Word64 -> Word16\n", "toWord16 = fromIntegral" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[43879,63289,58186,5831,38948]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "map toWord16 $ take 5 $ drop 1 $ iterate generatorA 65" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[54071,34184,58186,52231,10244]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "map toWord16 $ take 5 $ drop 1 $ iterate generatorB 8921" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "stream :: (Word64 -> Word64) -> Word64 -> [Word16]\n", "stream gen n0 = map toWord16 $ drop 1 $ iterate gen n0" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "588" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "length $ filter (uncurry (==)) $ take 40000000 $ zip (stream generatorA 65) (stream generatorB 8921)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "631" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "length $ filter (uncurry (==)) $ take 40000000 $ zip (stream generatorA 873) (stream generatorB 583)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "filteredStream :: Word16 -> [Word16] -> [Word16]\n", "filteredStream f stream = filter (\\n -> n .&. f == 0) stream" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[38948,48816,54372,43440,40536]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "take 5 $ filteredStream 3 $ stream generatorA 65" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "38948" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "toWord16 1352636452" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1352636452,1992081072,530830436,1980017072,740335192]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "take 5 $ filter (\\n -> n .&. 3 == 0) $ iterate generatorA 65" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1233683848,862516352,1159784568,1616057672,412269392]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "take 5 $ filter (\\n -> n .&. 7 == 0) $ iterate generatorB 8921" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "solE = length $ filter (uncurry (==)) $ take 5000000 $ zip fsA fsB\n", " where fsA = filteredStream 3 $ stream generatorA 65\n", " fsB = filteredStream 7 $ stream generatorB 8921" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "309" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "solE" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "sol = length $ filter (uncurry (==)) $ take 5000000 $ zip fsA fsB\n", " where fsA = filteredStream 3 $ stream generatorA 873\n", " fsB = filteredStream 7 $ stream generatorB 583" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "279" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sol" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Haskell", "language": "haskell", "name": "haskell" }, "language_info": { "codemirror_mode": "ihaskell", "file_extension": ".hs", "name": "haskell", "version": "8.0.2" } }, "nbformat": 4, "nbformat_minor": 2 }