Project Euler 92

2024-06-14 15:58
文章标签 project euler 92

本文主要是介绍Project Euler 92,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Square digit chains

Problem 92

A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before.

For example,

44 → 32 → 13 → 10 → 1 → 1
85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89

Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. What is most amazing is that EVERY starting number will eventually arrive at 1 or 89.

How many starting numbers below ten million will arrive at 89?


0.57s,重复组合数什么的。(F 的 120s 真的太彪悍了。)

import itertools
import timedef conquer():ans = 0DIGIT_LIMIT = 7ITER_STR = "0123456789"sum_square = lambda ss: sum( int( s ) ** 2 for s in str( ss ) )fact = lambda num: reduce( lambda x, y: x * y, xrange( 1, num + 1 ) )combinations = itertools.combinations_with_replacement( ITER_STR, DIGIT_LIMIT )def combinatorial_num( num ):res = fact( DIGIT_LIMIT )s = str( num )for ss in ITER_STR:count = s.count( ss )if count > 1: res /= fact( count )return resfor c in combinations:num = int( "".join( c ) )if num == 0: continuewhile num != 89 and num != 1:num = sum_square( num )if num == 89:ans += combinatorial_num( c )print ansbegin = time.time()
conquer()
end = time.time()
print end - begin


这篇关于Project Euler 92的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1060856

相关文章

Java compiler level does not match the version of the installed Java project facet. map解决方法

右键项目“Properties”,在弹出的“Properties”窗口左侧,单击“Project Facets”,打开“Project Facets”页面。 在页面中的“Java”下拉列表中,选择相应版本就OK了。

MyEclipse:Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project。

Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project 'myf'. java.lang.NullPointerException 此问题一般发生在Myeclipse 保存文件并自动部署时候。 Errors occurred during the buil

使用Qt Creator时遇到错误“Project ERROR: Xcode not set up properly”的解决方案

最近我在macOS上首次安装了Qt 5.5,并使用Qt Creator开发。然而每次启动或打开项目时,都会遇到以下错误提示: Project ERROR: Xcode not set up properly. You may need to confirm the license agreement by running /usr/bin/xcodebuild. 然后当我在终端执行 /usr

eclipse 如何创建一个Dynamic Web project (动态web项目)

1.准备工作: eclipse的下载安装 2.创建Dynamic Web project 至此一个Dynamic web project生成完毕。 项目结构为:

【CT】LeetCode手撕—92. 反转链表 II

目录 题目1- 思路2- 实现⭐92. 反转链表 II——题解思路 3- ACM实现 题目 原题连接:92. 反转链表 II 1- 思路 模式识别:翻转 给定 left 和 right 固定区间的链表 ——> ①虚拟头结点 + ②三个指针方式实现 2- 实现 ⭐92. 反转链表 II——题解思路 class Solution {public ListNode

基于REST的web service project添加spring框架

最近写项目,要使用spring3+hibernate4开发REST ful的web service project,框架都添加进去以后,使用spring注解方式获得对象实例永远是个NULL,急了一天终于是弄出来了,现做如下总结。 在web.xml中默认的JAX-RSServlet服务所对应的类完整路径是com.sun.jersey.spi.container.servlet.ServletCon

2024.1版 IDEA share project on github 报错

2024.1版 IDEA share project on github 报错 报错信息报错原因解决办法 报错信息 Cannot load information for github.com/Worldfickler:Request response: Access tothis site has been restricted. lf you believe this is

导入第三包勾选project是灰色的解决

今天遇到个问这个问题的,顺便帮解决了,是在导入工程的时候出现的,这是因为有同名的工程的,进入 windows->show view->project explorer 这里找出来删掉再导入工程即可。

[Project]心得

第一个项目:BBS,借此机会说说自己的一点感受及体会:首先感觉最深的是,通过这个项目把零散的知识点串结了起来,对做一个较大一点的东西有了更清晰的认识及思路,也多了一份信心,对于同一个问题就可以产生很多种不同的方法。以前对数据库或者字符串以及数组的操作都是很简单的知道可以用对应函数什么的来处理,但对于一个项目来说,用到的值很复杂,情况也特殊的多,很多情况都是多个函数相互处理对应产生的结果,如果你是高

Gradle 自动化项目构建-Gradle 核心之 Project

一、前言 从明面上看,Gradle 是一款强大的构建工具,但 Gradle 不仅仅是一款强大的构建工具,它更像是一个编程框架。Gradle 的组成可以细分为如下三个方面: groovy 核心语法:包括 groovy 基本语法、闭包、数据结构、面向对象等等。Android DSL(build scrpit block):Android 插件在 Gradle 所特有的东西,我们可以在不