ตัวเลข x ที่ x ^ 2 หาร 7 ^ x-1


16

งาน

มีชุดของตัวเลขที่เป็นxเช่นว่าแบ่งx^27^x-1

งานของคุณคือหาตัวเลขเหล่านี้ รับอินพุตของ n รหัสจะพิมพ์หมายเลขที่ n ตามกฎนี้

ตัวอย่าง 1-index

In   Out
3    3
9    24
31   1140

ลำดับที่เกี่ยวข้องสามารถพบได้ ที่นี่

กฎระเบียบ

คำตอบที่สั้นที่สุดจะเป็นผู้ชนะ *

ใช้กฎมาตรฐานการตีกอล์ฟ

ช่องโหว่ไม่อนุญาต

คำตอบของคุณอาจเป็นดัชนี 0 หรือ 1 โปรดระบุในคำตอบของคุณ


@nimi ฉันเขียนสิ่งเหล่านี้ลงไปเมื่อวางแผนและไม่เคยใช้งานเลย ฉันได้อัพเดทคำถาม
george

ขีด จำกัด ของnอะไร ฉันสามารถให้ผลลัพธ์ที่ถูกต้องด้วยn=9แต่n=10ทำให้ฉันมีปัญหาอยู่แล้ว
ต้มตุ๋น

@briantist หากคุณได้รับผลลัพธ์ที่ผิดสำหรับค่าอินพุตที่สูงขึ้นคำตอบของคุณไม่ถูกต้อง หากใช้เวลานานอาจขึ้นอยู่กับการใช้งาน
mbomb007

มันใช้เวลาไม่นาน n=10ให้ฉัน 32; มันเป็นเพราะมันเริ่มใช้ double แทนที่จะเป็นจำนวนเต็มและ mod ผิดหลังจากนั้น :(
นักเทศน์

คำตอบ:


8

Haskell, 34 ไบต์

([x|x<-[1..],mod(7^x-1)(x^2)<1]!!)

สิ่งนี้ใช้การจัดทำดัชนีแบบ 0 ตัวอย่างการใช้: ([x|x<-[1..],mod(7^x-1)(x^2)<1]!!) 30->1140 ->

เป็นการใช้คำจำกัดความโดยตรง มันสร้างรายการของตัวเลขทั้งหมดxและเลือกnth


5

Pyth , 10 ไบต์

e.f!%t^7Z*

โปรแกรมที่รับอินพุตของจำนวนเต็มและพิมพ์ค่าที่จัดทำดัชนีไว้หนึ่งรายการ

ลองออนไลน์!

มันทำงานอย่างไร

e.f!%t^7Z*     Program. Input: Q
e.f!%t^7Z*ZZQ  Implicit variable fill
               Implicitly print
e              the last
 .f         Q  of the first Q positive integers Z
     t^7Z      for which 7^Z - 1
    %          mod
         *ZZ   Z^2
   !           is zero

5

JavaScript (ES7), 40 ไบต์

f=(n,i=1)=>n?f(n-!((7**++i-1)%i**2),i):i

นี้สูญเสียความแม่นยำเป็นธรรมอย่างรวดเร็วเนื่องจากความจริงที่ว่า JS 7**19สูญเสียความแม่นยำโดย นี่เป็นเวอร์ชั่น ES6 ที่เกือบจะมีความแม่นยำโดยพลการ:

f=(n,i=0)=>n?f(n-!(~-(s=++i*i,g=j=>j?g(j-1)*7%s:1)(i)%s),i):i

สิ่งนี้จะเสร็จสิ้นภายในประมาณหนึ่งวินาทีสำหรับกรณีทดสอบ 31

อีกสองสามแนวทาง:

f=(n,i=0)=>n?f(n-!(~-(s=>g=j=>j?g(j-1)*7%s:1)(++i*i)(i)%s),i):i
f=(n,i=0)=>n?f(n-!(s=++i*i,g=(j,n=1)=>j?g(j-1,n*7%s):~-n%s)(i),i):i
f=(n,i=0)=>n?f(n-!(s=>g=(j,n=1)=>j?g(j-1,n*7%s):~-n%s)(++i*i)(i),i):i

4

05AB1E , 11 ไบต์

µ7Nm<NnÖiN¼

ลองออนไลน์!

ด้วยเหตุผลบางอย่างฉันไม่สามารถ½ทำงานได้µ7Nm<NnÖ½Nหรือฉันจะผูกติดกับ Pyth

µ           # Loop until the counter equals n.
 7Nm<       # Calc 7^x+1.
     Nn     # Calc x^2.
       Ö    # Check divisibility.
        iN¼ # If divisible, push current x and increment counter.
            # Implicit loop end.
            # Implicitly return top of stack (x)

.


ใช่ฉันเคยเห็นว่าÖในรายการแก้ไขของฉันเป็นเวลาหลายเดือน แต่ฉันไม่เคยได้รับการจัดการกับมัน อย่างไรก็ตามคุณไม่จำเป็นต้องใช้Nเมื่อµผลลัพธ์ออกมาโดยอัตโนมัติNหากสแต็กว่างเปล่า
Emigna

4

Python 2 , 48 46 bytes

ขอบคุณ @Dennis สำหรับ -2 ไบต์!

f=lambda n,i=1:n and-~f(n-(~-7**i%i**2<1),i+1)

ฟังก์ชันเรียกซ้ำแบบดัชนีที่รับอินพุตผ่านอาร์กิวเมนต์และส่งคืนผลลัพธ์

ลองออนไลน์!(เพิ่มขีด จำกัด การเรียกซ้ำเพื่อให้กรณีทดสอบขั้นสุดท้ายทำงานได้)

มันทำงานอย่างไร

n เป็นดัชนีที่ต้องการและ iเป็นตัวแปรการนับ

การแสดงออก~-7**i%i**2<1กลับTrue(เทียบเท่า1) ถ้าi^2หาร7^i - 1และFalse(เทียบเท่า0) มิฉะนั้น แต่ละครั้งที่เรียกใช้ฟังก์ชันผลลัพธ์ของนิพจน์จะถูกลบออกnลดลงnทุกครั้งที่พบ Hitiจะเพิ่มขึ้นด้วย

พฤติกรรมการลัดวงจรของandหมายความว่าเมื่อnเป็น0, 0จะถูกส่งกลับ; นี่คือกรณีฐาน เมื่อถึงจุดนี้การเรียกซ้ำจะหยุดลงและค่าปัจจุบันของiจะถูกส่งกลับโดยการเรียกใช้ฟังก์ชันดั้งเดิม แทนที่จะใช้อย่างชัดเจนiนี้จะกระทำโดยใช้ความจริงที่ว่าสำหรับการโทรแต่ละฟังก์ชั่นการเพิ่มขึ้นได้รับการดำเนินการโดยใช้-~ในด้านหน้าของสาย; 0 iเวลาที่เพิ่มขึ้นให้iตามที่ต้องการ


1
(~-7**i%i**2<1)บันทึกสองไบต์
Dennis

@ เดนนิสแน่นอน! ขอบคุณ
TheBikingViking

3

Python 2 , 57 53 51 ไบต์

-4 ไบต์ขอบคุณ ETHproductions
-2 ไบต์ขอบคุณ TuukkaX

i=0
g=input()
while g:i+=1;g-=~-7**i%i**2<1
print i

ลองออนไลน์!
ลำดับคือ 1-indexed


@ETHproductions yep c:
Rod

ตู้ทดสอบล้มเหลวหรือไม่หากคุณลบวงเล็บออก(7**i)? ฉันลบมันออกไปและใช้งานได้กับสิ่งที่ฉันลอง
Yytsi

@TuukkaX แน่นอน**มีความสำคัญสูงกว่า~และ-
Rod

2

Python 2, 57 ไบต์

สิ่งนี้ใช้เวลานานมากสำหรับค่าที่ยิ่งใหญ่ นอกจากนี้ยังใช้หน่วยความจำมากมายเพราะมันสร้างรายการทั้งหมดไกลเกินความจำเป็น ผลลัพธ์ที่ได้จะถูกทำดัชนีเป็นศูนย์

lambda n:[x for x in range(1,2**n+1)if(7**x-1)%x**2<1][n]

ลองออนไลน์


จากความอยากรู้มีข้อพิสูจน์สำหรับ2**n+1ขีด จำกัด บนหรือไม่?
ร็อด

@Rod ไม่ว่าฉันรู้ แต่การพิจารณาว่ามีค่า 50 <5000 ผมแน่ใจว่ามีมากขึ้นกว่า 50 2**50< ฉันสามารถใช้งาน9**n+9ได้ แต่ใช้เวลานานกว่านั้นมาก ฉันเริ่มทำงานเมื่อf(20)ไม่นานมานี้ (พร้อม2**n+1); มันยังไม่เสร็จสมบูรณ์
mbomb007

ฉันไม่คิดว่ามันจะมีข้อพิสูจน์ว่าซีเควนซ์นั้นไม่มีขีด จำกัด นับเป็นขอบเขตที่ดีสำหรับคำที่ n!
เกร็กมาร์ติ

2

Mathematica ขนาด 43 ไบต์

ขณะนี้ฉันมีวิธีแก้ไขปัญหาสามอย่างที่นับจำนวนไบต์นี้:

Nest[#+1//.x_/;!(x^2∣(7^x-1)):>x+1&,0,#]&
Nest[#+1//.x_/;Mod[7^x-1,x^2]>0:>x+1&,0,#]&
Nest[#+1//.x_:>x+Sign@Mod[7^x-1,x^2]&,0,#]&

What is the character between x^2 and (7^x... in the first line? It looks like a pipe but shorter
Sefa

@Sefa It's the Unicode character for the mathematical "divides" symbol and is used by Mathematica as an operator for Divisible.
Martin Ender

Here is one at 41 Bytes: Cases[Range[#^3],x_/;x^2∣(7^x-1)][[#]]& based on the heuristic argument that n^3 is an upper bound. I have discovered a truly marvelous proof of this, which this margin is too narrow to contain :)
Kelly Lowder



1

R, 35 bytes

This only works for n<=8.

z=1:20;which(!(7^z-1)%%z^2)[scan()]

However, here's a longer version which works for n<=25, for 50 bytes:

z=1:1e6;which(gmp::as.bigz(7^z-1)%%z^2==0)[scan()]

Does this only work up to 8 because it become a long int?
george

1
@george Yes, you lose accuracy as R defaults to 32 bit integers. The second version of the code uses a package, gmp, which allows arbitrarily large integers. However, I rapidly run out of RAM for computing anything above n=25.
rturnbull

0

PHP, 47 49 bytes

while($n<$argv[1])$n+=(7**++$x-1)%$x**2<1;echo$x;

Only works for n<9 (7**9 is larger than PHP_INT_MAX with 64bit)

62 bytes using arbitrary length integers: (not tested; PHP on my machine doesn´t have bcmath)

for($x=$n=1;$n<$argv[1];)$n+=bcpowmod(7,++$x,$x**2)==1;echo$x;

Run with php -nr '<code>' <n>.

pseudo code

implicit: $x = 0, $n = 0
while $n < first command line argument
    increment $x
    if equation is satisfied
        increment $n
print $x


0

Clojure, 83 bytes

(fn[n](nth(filter #(= 0(rem(-(reduce *(repeat % 7N))1)(* % %)))(iterate inc 1N))n))

Try it online!

This builds an infinite list of Java BigIntegers starting at 1 and filters them by the definition. It uses zero-based indexing to select the nth value from the filtered list.



0

Powershell, too many bytes

Just to see if it was possible and it is.

[System.Linq.Enumerable]::Range(1,10000)|?{[System.Numerics.BigInteger]::Remainder([System.Numerics.BigInteger]::Pow(7,$_)-1,$_*$_) -eq 0}


0

QBIC, 39 bytes

:{~(7^q-1)%(q^2)=0|b=b+1]~b=a|_Xq\q=q+1

I couldn't get it to run in QBasic 4.5, but it seems to run fine in QB64. For some inexplicable reason, QBasic refuses to cleanly divide 13,841,287,200 by 144, but instead gives a remainder of -128. It then returns 16 as the 7th term of this sequence instead of 12...

:{      get N from the command line, start an infinite DO-loop
~       IF
(7^q-1) Part 1 of the formula (Note that 'q' is set to 1 as QBIC starts)
%       Modulus
(q^2)   The second part
=0      has no remainder
|b=b+1  Then, register a 'hit'
]       END IF
~b=a    If we have scored N hits
|_Xq    Quit, printing the last used number (q)
\q=q+1  Else, increase q by 1. 
        [DO-loop and last IF are implicitly closed by QBIC]

0

Wonder, 28 bytes

@:^#0(!>@! % - ^7#0 1^#0 2)N

Zero-indexed. Usage:

(@:^#0(!>@! % - ^7#0 1^#0 2)N)2

Filters from the list of natural numbers with a predicate that determines whether x^2 is divisible by 7^x-1, then gets the nth item in that list.


โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.