Skip to content

Instantly share code, notes, and snippets.

import java.math.BigInteger
fun main() {
val factors = semiprimeFactors(BigInteger("769817083110377"))
println("Factors: $factors") // Factors: (13685611, 56250107)
}
/**
* Fermat's factorization method: https://en.wikipedia.org/wiki/Fermat%27s_factorization_method
* Semiprime n = a^2 - b^2 = (a - b) * (a + b)
@danialgoodwin
danialgoodwin / BillingManager.kt
Created May 26, 2019 02:52
Simple Kotlin example of using Google's in-app billing v2 for subscriptions (for one-time payments, generally change 'SUBS' with 'INAPP')
package dev.goodwin
import android.app.Activity
import android.util.Log
import com.android.billingclient.api.*
import com.android.billingclient.api.BillingClient.BillingResponseCode
import com.android.billingclient.api.BillingClient.FeatureType
import dev.goodwin.BillingManager.Companion.formatPeriod
/**
@danialgoodwin
danialgoodwin / RadioButtonPreference
Created August 8, 2016 23:40
Android Custom Preferences
/**
* Copyright (c) 2016 Danial Goodwin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/core-ajax/core-ajax.html">
<polymer-element name="my-element" noscript>
<template>
<span>I'm <b>my-element</b>. This is my Shadow DOM.</span>
<core-ajax url="http://example.com/json" auto response="{{resp}}"></core-ajax>
<textarea value="{{resp}}"></textarea>
</template>
</polymer-element>
public class Sample {
public static void main(String[] args) {
List<Integer> values = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
int result = 0;
for (int e : values) {
if (e > 3 && e % 2 == 0) {
result = e * 2;
break;
}
@danialgoodwin
danialgoodwin / AndroidManifest.xml
Last active August 29, 2015 14:13
DemoAndroidAnnotations
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.simplyadvanced.testandroidannotations" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
@danialgoodwin
danialgoodwin / PrimeNumbers.cpp
Created June 15, 2013 20:12
Get prime numbers using the Sieve of Eratosthenes. Also, finds the sum of all the primes below one million. Used for Project Euler.
// Finds the sum of all the primes below one million.
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
const int NUM = 1000000;
int count = 1;
@danialgoodwin
danialgoodwin / ActivityMain.java
Last active April 9, 2016 18:38
There is a bug in Android's TimePicker for 4.0+. The onTimeChanged() callback in the onTimeChangedListener() is not called when the user changes am/pm. This sample code shows a workaround for the Android TimePicker in 4.0+, demonstrating a TextView that is always in sync with the TimePicker, regardless of which vertical spinner is being manipula…
// This is for API 15-17+
// Tested on 4.0.3
public class ActivityMain extends Activity {
ImageButton buttonTimeDone;
NumberPicker numberPickerAmPm;
TextView textViewTime;
TimePicker timePickerMain;
@Override
protected void onCreate(Bundle savedInstanceState) {