Dp:humble Numbers, ugly number

Source: Internet
Author: User

Description A number whose only prime factors be 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, ten,, +, (+), A, ten, A, ten, ten, ... shows the first humble numbers.

Write a program to find and print the nth element in this sequence.
Enter the input consists of one or more test cases. Each test case consists the one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for N. Output for each test case, print one line saying "The nth humble number is num ber. ". Depending on the value of n, the correct suffix "st", "nd", "ts", or "th" for the ordinal number nth have to is used like I T is shown in the sample output. Sample input
1234111213212223100100058420
Sample output
The 1st humble number is 1.The 2nd humble number is 2.The 3rd humble number are 3.The 4th humble number is 4.The 11th HUMBL E number is 12.The 12th humble number are 14.The 13th humble number is 15.The 21st humble number is 28.The 22nd humble numb ER is 30.The 23rd humble number is 32.The 100th humble number are 450.The 1000th humble number is 385875.The 5842nd humble Number is 2000000000.

Ideas:

The first ugly number can be obtained by one of the previous ugly numbers.
So the first idea: when I was asked for an ugly number, enumeration 1 to i-1 the number of ugly, the num[j]* (2/3/5/7) (1<=j<=i-1, and this number >num[i-1]) the minimum value
This idea is n^2, and obviously it will time out.
So how can I quickly update the first i-1 by a few ugly numbers?
This is the idea of a constantly updated approach.
A indicates that the number of a ugly number may be the current requirement of the first I ugly
b~~~~~b~~~~~~3~~~~~~~~~~~~~~~~~~~~~~~~~
c~~~~~c~~~~~~5~~~~~~~~~~~~~~~~~~~~~~~~~
d~~~~~d~~~~~~7~~~~~~~~~~~~~~~~~~~~~~~~~

F[1]=1
A=1 b=1 c=1 d=1
F[i]=min (F[A]*2,F[B]*3,F[C]*5,F[D]*7)
If F[i is currently evaluated by a, then Inc (a);
If ~~~~~b~~~~~~~~~~~~~~~~~~b
~~~~~~~~~c~~~~~~~~~~~~~~~~~~c
~~~~~~~~~d~~~~~~~~~~~~~~~~~~d

Do not understand English, not enough to walk the lake.
The output ordinal word in question is a pit point.
1:st 1001:st
2:nd 1002:nd
3:rd 1003:rd
11:th 1011:th
12:th 1012:th
13:th 1013:th



Code
var f:array[0..5842]of int64;
N,i,j,k:longint;
A,b,c,d:longint;
Min,minj:longint;
Init:array[0..5842]of Longint;
Mm:longint;
Begin F[1]:=1; f[0]:=0;
A:=1; B:=1;
C:=1; D:=1;
For i:=2 to 5842 do
Begin Min:=maxlongint;
if (f[a]*2<min) and (F[a]*2>f[i-1])
Then min:=f[a]*2;
if (f[b]*3<min) and (F[b]*3>f[i-1])
Then min:=f[b]*3;
if (f[c]*5<min) and (F[c]*5>f[i-1])
Then min:=f[c]*5;
if (f[d]*7<min) and (F[d]*7>f[i-1])
Then min:=f[d]*7;
F[i]:=min;
If F[a]*2=min then Inc (a);
If F[b]*3=min then Inc (b);
If F[c]*5=min then Inc (c);
If F[d]*7=min then Inc (d);
End
READLN (n);
While n<>0 do
Begin write (' the ', N);
if (n mod 10=1) and (n mod 100<>11) then write (' St ')
else if (n mod 10=2) and (n mod 100<>12) then write (' nd ')
else if (n mod 10=3) and (n mod 100<>13) then write (' Rd ')
else write (' th ');
                  Writeln (' Humble number is ', f[n], '. ');
READLN (n);
End
End.

Dp:humble Numbers, ugly number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.